I am looking for a JSON library that supports defining the serialization depth. Ideally I would like a library that handles all primitive types and java.lang and the rest will be sent to a custom handler. In my case the custom handler should generate a Link object with URL and class of the element (for my REST API).
Here an example:
Person : String name, Car car
Would be serialized to
{
“name”:”Peter”,
Link : {“class”:”my.company.Car”, “url”:”http://www.mycompany/myapp/Car/5”}
}
Any ideas which library I could use (and enhance)?
Kind regards,
Daniel
Check out http://code.google.com/p/google-gson/
You want to have a look at Jackson.
Jackson Wiki
Jackson in 5 Minutes
AS you can see you can use simple and complex data-binding rules, and there's a streaming API which will allow you to limit the exploration's depth.
If GSON does not fit your needs, I recommend JsonMarshaller. It is highly configurable and customizable yet strives to be simple. It also has a very active user and developer community.
I am not sure whether you want actual control for serialization depth (as mentioned) or not -- your explanation rather suggest you want to be able to add custom serializers.
Now: if you really need limits (like only shallow copy), you could check out FlexJson, which supposedly has explicit control over serialization depth.
Otherwise, Jackson and GSON at least have full bean serialization as well as allowing custom serializers.
Related
I would like to output the very same class with different set of fields in different endpoints.
Is it possible?
Yes you can do that with Jackson's #JsonView support in RESTEasy Reactive.
See this for more details.
For even more advanced customization, you can use #CustomSerialization, as mentioned here.
No, but you can output string. You will have full control over serialization that way.
I'm writing a basic SDK for Java & Python (and potentially other languages in the future) consumers of an API (REST as well as message-queue orientated responses).
Instead of maintaining separate per-language descriptions, I was wondering if there was perhaps a way to define classes and enums in something like YAML which could be automatically be converted into appropriate objects in each language.
I imagine I could write the objects in C and then make per-language bindings - but this seems a bit hacky as a solution.
You can use JSON to represent your object model and translate it to objects in each of the language you are using.
Here are some relevant articles that will help you get started.
How to convert Java object to / from JSON (Jackson)
https://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
https://realpython.com/python-json/#encoding-and-decoding-custom-python-objects
Working With JSON Data in Python
Another option is Protocol Buffers though it is not as favorite as JSON when working with REST apis.
I'm returning to backend development after a few years focusing on iOS. I've chosen Spring Boot with Kotlin.
I would like to have a domain-centric, general, reusable model objects.
For service invocations I would like to return use-cases specific responses where the payload in/out is just the information for that particular scenario. (We can call this "contract-first" or "don't spill your guts")
In the past I might've implemented a service by mapping the information from reusable model objects onto a serializable value object using a framework like Dozer.
Alternatively, I was thinking of registering a custom serializer for a given endpoint, as eg Django Rest Framework does. (Not sure how this would work given jackson serializes by annotations).
Questions:
Is there an easy way to use custom serializers with jackson to do what I want?
Or is it better to continue using the value-object approach I used years ago?
Depending on your use case, using dedicated DTOs could be a good idea. Just so you know, you can get more flexibility from Jackson without implementing custom serializers for each scenario - Jackson Json Views are now supported in Spring MVC and quite useful.
I need to render JSON data in my JSPs for some AJAX requests. I'd like to know what's the best way to do it in terms of easy to use and stability.
Assuming you want to generate the JSON from one or more Java objects, the following is a fairly straightforward approach:
Set the Java object(s) as attributes in request/session scope
Convert the objects to JSON using a tag library such as http://json-taglib.sourceforge.net/index.html
I'm assuming you have checked out json.org already...
I recently switched from json-lib to XStream:
http://x-stream.github.io/
Definitely much easier. Just a few lines of code and you are done. Note, it started as an XML serializer, deserializer. It now supports JSON ouput.
Does somebody know a Java library which serializes a Java object hierarchy into Java code which generates this object hierarchy? Like Object/XML serialization, only that the output format is not binary/XML but Java code.
Serialised data represents the internal data of objects. There isn't enough information to work out what methods you would need to call on the objects to reproduce the internal state.
There are two obvious approaches:
Encode the serialised data in a literal String and deserialise that.
Use java.beans XML persistence, which should be easy enough to process with your favourite XML->Java source technique.
I am not aware of any libraries that will do this out of the box but you should be able to take one of the many object to XML serialisation libraries and customise the backend code to generate Java. Would probably not be much code.
For example a quick google turned up XStream. I've never used it but is seems to support multiple backends other than XML - e.g. JSON. You can implement your own writer and just write out the Java code needed to recreate the hierarchy.
I'm sure you could do the same with other libraries, in particular if you can hook into a SAX event stream.
See:
HierarchicalStreamWriter
Great question. I was thinking about serializing objects into java code to make testing easier. The use case would be to load some data into a db, then generate the code creating an object and later use this code in test methods to initialize data without the need to access the DB.
It is somehow true that the object state doesn't contain enough info to know how it's been created and transformed, however, for simple java beans there is no reason why this shouldn't be possible.
Do you feel like writing a small library for this purpose? I'll start coding soon!
XStream is a serialization library I used for serialization to XML. It should be possible and rather easy to extend it so that it writes Java code.