How to serialize ArrayList<ObjectId> using Jackson - java

Is there a way to tell Jackson to serialize an ArrayList<ObjectId> in an object?
Currently what I did is create a new Class containing the id and uses it instead like ArrayList<SomeObject>
class SomeObject {
#JsonSerialize(using = ToStringSerializer.class)
private final ObjectId id;
}
It works fine but I want to know if there's a way for Jackson to serialize an ArrayList<ObjectId> without created a new class?

Related

Spring Data Mongodb : Deserialize using builder

I'm wondring if there's a way to configure spring data to use a builder to deserialise a document. Something like JsonPOJOBuilder or JsonDeserialize(builder = ADocumentBuilder) for jackson ?
In fact I'm using lombok with #SuperBuilder annotation like :
#Data
#RequiredArgConstrcutor
#SuperBuilder
public abstract class AttachementBase {
private String a;
private final String b; //maybe final
private String c;
}
#Value
#SuperBuilder
#Document
public class Attachement extend AttachementBase{
private String d;
}
Evething's works fine with the builder and how to use it, except when calling the AttachementRepository.findXX() Spring trys to call a constructor but it cannot find the appropriate one.
Lombok generates AttachementBuilder in the Attachement class and I want to use it to deserialise this document.
I do not want this post be related to lombok, i juste illustrated the use case. The question is how to use a builder to deserialize a document in Spring-data-mongodb ?

Using transient for both Gson and Hibernate in the same file

I have entity that I need some of the fields not to be persisted and some of the fields not to be serialized.
I am using the #Transient on some of the fields but when I want to mark transient for the Gson. The issue is that hibernate picks it up and also not persist it since its also a keyword in Hibernate .
I use Hibernate-jpa-2.1-api javax.persistence.Transient
I am trying prevent addresses from being serialized and getDefaultAddress should not be saved.
Code:
#Entity
#Table(name="Business")
public class Business{
#OneToMany(mappedBy="business")
private transient List<Phone> addresses;
#Transient
public Phone getDefaultPhone() {
return phones.get(0);
}
}
Any solution?
You can use #Expose
#Expose(serialize = false)
#OneToMany(cascade=CascadeType.ALL, mappedBy="business")
private List<Address> addresses;
In order to be able to work with this annotation,
final GsonBuilder builder = new GsonBuilder();
builder.excludeFieldsWithoutExposeAnnotation();
final Gson gson = builder.create();
Source : http://www.javacreed.com/gson-annotations-example/
I am using Play Framework's implementation of JPA, and Gson.
I got it working with JPA class as follows, which involved constructing the embedded object dynamically before marshalling to json, and no changes were required for the GSON builder, as modifying GSONBuilder will change the strategy globally for other classes as well.
public class DBServiceDefinition {
#Transient
#Expose(serialize = true)
#SerializedName("serviceStatus")
public ServiceDefinitionStatus status = new
ServiceDefinitionStatus(201,"SUCCESS","","Test");
}

XML serialization of nested objects in JAVA

I am using JAXB to serialize and deserialize java objects.
Classes are as:
public class Level1Class
{
String Id;
HashMap<String,Level2Class> level2Map = new HashMap<String,Level2Class>();
}
public class Level2Class
{
String Id;
}
I want to serialize and deserialize object of Level1Class.
Please suggest annotation for classes and fields.
What else do I need to include
1. default constructor
2. getter setter of fields
To make an object serializable, you just have to let it implement Serializable and make sure that every field of this object is serializable as well.

Is it possible to use one class for both ORMLite and Jackson JSON?

I want to use ORMLite to query data from SQLite and store it in Java class, then convert this class to JSON using Jackson JSON library and send it through HTTP. I also want to do opposite - get data from server in JSON and convert it to Java class and save this class to SQLite using ORMLite.
Can I do this using one class per table for both ORMLite and Jackson?
Yes you can, why not? you can convert to json any java object you want
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);;
ORMLite create entity, which is java object so.
#DatabaseTable(tableName = "accounts")
public class Account {
#DatabaseField(id = true)
private String name;
#DatabaseField(canBeNull = false)
private String password;
...
Account() {
// all persisted classes must define a no-arg constructor with at least package visibility
}
...
}
yes you can.

Jackson: deserialization to a collection

I have this specific problem with JSON deserialization. Let's have this JSON structure:
{
"header":{
"objects":[
{"field":"value1"},
{"field":"value2"}
]
}
}
The JSON structure can't be altered as it comes from a 3rd party system.
Now let's have this simple POJO:
#JsonDeserialize(using=PojoDeserializer.class)
public class Pojo {
private string field;
//...getter, setter
}
The mentioned PojoDeserializer takes {"field": "value"} json string and deserializes it to the Pojo instance. So I can simply do the deserialization like this
Pojo instance = new
ObjectMapper().readValue("{\"field\":
\"value\"}", Pojo.class);
And here's my problem. Let's have another deserializer PojosCollectionDeserializer which takes the mentioned structure and deserializes it to a Collection of Pojo instances. I'd like to use it in a similar fashion as in the previous example:
Collection<Pojo> pojos = new ObjectMapper().readValue("{...}", Collection.class);
But this doesn't work as there is not defined that Collection should be created using the PojosCollectionDeserializer. Is there any way to achieve it?
I am not sure why are trying to explicitly specify deserializers, as it would all work just fine with something like:
public class Message {
public Header header; // or, if you prefer, getter and setter
}
public class Header {
public List<Pojo> objects;
}
public class Pojo {
public String field;
}
Message msg = objectMapper.readValue(json, Message.class);
without any additional configuration or annotations. There is no need to construct custom serializers or deserializers for simple cases like this.

Categories

Resources