How to use an Eclipselink Converter to serialize a list of objects to json - eclipselink

I've written a implementation of Converter that uses Jackson JSON to serialize a list of deeply structured objects to JSON, but it's not being called. Am I doing this correctly?
#Converter(name="arrayList", converterClass=ArrayListJsonSerializedObjectConverter.class)
private List<DeeplyStructuredObject> deeplyStructuredObjects= new ArrayList<DeeplyStructuredObject>();
Additionally, I'd like to know whether or not one can use a ReadTransfomer in addition to a
Converter on the same property, e.g.
#Converter(name="arrayList", converterClass=ArrayListJsonSerializedObjectConverter.class)
#ReadTransformer(method="someMethod")
private List<DeeplyStructuredObject> deeplyStructuredObjects= new ArrayList<DeeplyStructuredObject>();

Related

How to (de)serialize an instance of a generated data class

I need to (de)serialize to and from JSON some of the data classes that are generated by a Gradle plugin. Normally I would just use a library like Moshi or kotlinx.serialization and add the proper annotation to the class I want to serialize but, since this data classes are autogenerated, this is a problem.
I would like to avoid manually to map all the fields of the generated data class to some other class that I can (de)serialize, or to write a custom adapter for all these data class so, I was wondering if there is another way to tell, for example, kotlinx.serialization that a class is #Serializable without having to put the annotation directly on top of the class itself.
Or, alternatively, is there a better way to convert to and from a string an instance of a generated data class?
kotlinx.serialization supports generating serializers for 3rd party classes. We need to use forClass parameter in #Serializer, for example:
data class MyData(val data: String)
#Serializer(forClass = MyData::class)
object MyDataSerializer
fun main() {
val data = MyData("foo")
println(Json.encodeToString(MyDataSerializer, data))
}
You can read more in the official documentation: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#deriving-external-serializer-for-another-kotlin-class-experimental

Jackson JSON difference between JsonNode and ObjectNode

I am using Jackson for JSON parsing.
What is the difference between JsonNode and ObjectNode?
And which to use for mapping JSON in string format.
Quick answer
JsonNode: Abstract class, used when reading a JSON document.
ObjectNode: Concrete implementation, used when building or modifying a JSON document.
Keep reading for a more detailed answer.
JsonNode
JsonNode is an abstract class used as the base class for all JSON nodes, which form the basis of JSON Tree Model that Jackson implements.
Quoting the JsonNode documentation:
As a general design rule, most accessors (getters) methods are included in this base class, to allow for traversing structure without type casts.
Mutators methods (setters), however, need to be accessed through specific sub-classes (such as ObjectNode and ArrayNode).
This seems sensible because proper type information is generally available when building or modifying trees, but less often when reading a tree (newly built from parsed JSON content).
The JsonNode concrete implementations can be found in the com.fasterxml.jackson.databind.node package.
ObjectNode
ObjectNode is a concrete implementation of JsonNode that maps a JSON object, and a JSON object is defined as following:
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

Dynamic Schema & Deserialization with Protostuff

I'm using Protostuff in an attempt to serialize/deserialize objects of several different types for which no protobuf sources are available (it's a server-server RPC scenario). Serialization goes OK because I know the type of the object to serialize and can create the schema:
Schema schema = RuntimeSchema.getSchema(object.getClass());
Now, I use ProtobufIOUtil.toByteArray and get a byte array which I then pass to a remote server. However, I can't seem to deserialize this byte array in the remote server because I have no way to create a schema for an object of "unknown" type. Is there any way I can get past this and use Protostuff in the same way I would use Java's native serialization?
There are few solutions with common idea - serialize name of the class together with the data.
First one requires protostuff-runtime. You should create wrapper class with one field of type Object:
public class Wrapper {
public Object data;
}
Then you put your object to data field and serialize wrapper, protostuff-runtime will append class name to serialized form automatically, and later use it for deserialization.
If you want more control, then you can do similar thing without protistuff-runtime.
First, you need a wrapper class:
public class Wrapper {
public String clazz;
public byte[] data;
}
Then you should serialize your data to byte array, store it to wrapper, and then serialize wrapper instance.
On remote side, you deserialize Wrapper first, then get clazz field - it is the class you should use to deserialize data.

Automatically update Entity instance from class MVC 3

I've got a class that closely resembles one of my entities (I use the class for JSON de/serialization because the entity fails conversion to JSON, one of the known gotchyas of JSON + MVC).
Once I deserilize a JSON string into my object, is there a way to automatically update the associated Entity model instance. The property names are the same.
e.g.
'myDeserialized is the deserialized JSON object coming over the wire
Dim entityInstance As DLL.Person = db.getPersonById(myDeserialized.id)
myDeserialized.update(entityInstance)
Where this just goes through and takes all the properties in myDeserialized and updates the same named property in entityInstance?
Or is it possible to just iterate over a key-value pair of all the properties in an object/entity?
I'm looking for something like TryUpdateModel(), but that only works with forms, right? I don't want to tie my data to a form, that's all.
Thanks!
I don't know what type and version of EF you use, but there are very powerful functions for iterating on the entities properties. Which mean you can easily make your update function on the entity or its baseclass' partial (entityInstance.Update(myDesrialized)).
If you use EntityObject, then you can get an ObjectStateEntry for your entities and via this class you can very easily achieve this.
Please let us know what version and type of EF you use (POCO?)

Mapping a JSON-encoded string in the DB to a Dictionary property on my object, using NHibernate

I have an object that maintains a property bag of various properties. This is exposed through my object model as a Dictionary<string, string> and is stored in the database as a JSON-encoded string.
What is the best way to map this class using Fluent NHibernate?
Option 1: Map to a private field, do JSON translation in my object?
Should I map the property to a private string field, and then do serialization to/from JSON in my object itself?
Pro: NH need only know about mapping a single string to a text column which seems easy enough.
Con: My model has to track the dictionary and a string, simply to support persistence.
Option 2: Use some sort of Interceptor?
I haven't done anything with NH Interceptors before, but I know of them. Could I use an interceptor to do the Dictionary/JSON serialization, so that my model need only know about the dictionary?
Option 3: Use a different encoding strategy?
Is there a different encoding strategy, besides JSON, that NH supports natively and that I could use to serialize my dictionary?
In order to store your Dictionary as a single encoded column, use a IUserType. You would map the dictionary as a property using your IUserType. This blog post has a good example of implementing an IUserType.
Map( x => x.JsonDictionary )
.CustomTypeIs<MyUserType>();
You can also map this as a collection of values. This allows querying of individual values from the dictionary.
HasMany( x => x.JsonDictionary )
.WithTableName("entity_jsondictionary")
.KeyColumnNames.Add("entityid")
.Cascade.All()
.AsMap<string>(
index => index.WithColumn("name").WithType<string>(),
element => element.WithColumn("value").WithType<string>()
);