Commit c8957d94 authored by Matija Obreza's avatar Matija Obreza
Browse files

User JSON serializer

parent 8d81087b
......@@ -40,9 +40,13 @@ import net.sf.oval.constraint.NotNull;
import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.servlet.controller.rest.serialization.UserSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Entity
@Table(name = "user")
@JsonSerialize(using = UserSerializer.class)
public class User extends BusinessModel {
/**
......
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.servlet.controller.rest.serialization;
import java.io.IOException;
import java.util.Set;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.User;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public class UserSerializer extends JsonSerializer<User> {
@Override
public void serialize(User user, JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonProcessingException {
if (user == null) {
jgen.writeNull();
} else {
jgen.writeStartObject();
jgen.writeObjectField("uuid", user.getUuid());
jgen.writeObjectField("name", user.getName());
jgen.writeObjectField("email", user.getEmail());
Set<UserRole> roles = user.getRoles();
if (roles != null) {
jgen.writeArrayFieldStart("roles");
for (UserRole role : roles) {
jgen.writeObject(role);
}
jgen.writeEndArray();
}
jgen.writeEndObject();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment