Jackson Serialize / deserialize empty values - java

I want to serialize an object to JSON and ignore empty fields. for that I used
objectMapper
.setSerializationInclusion(Include.NON_NULL)
.setSerializationInclusion(Include.NON_EMPTY)
now I want the opposite way.
to be able to deserialize it back to object but without null but empty values.
using the mapper object.
I don't want to touch the class itself and I want it to work for any kind of class (to be generic).
is it possible?
Thanks.

Related

Serialize same type objects to the same file

I want objects of a same serializable class to serialize in the same file. I have tried to do it by putting everything in an array and then serialize by I want the objects to be serialized individually and saved into the same file
If you want to serialize multiple objects, why not serialize a collection of those objects? When you deserialize the collection you can access the objects again by iterating. Or if you have some unique identifer for the object you can put them in a map instead and serialize that.
Worth mentioning is that Java serialization will be going away in future with the newer versions. You are better off using a JSON serializer / de-serializer in my opinion, unless you of course are trying to hide the contents somehow. I use FasterXML myself and it works really great with POJOs.
Have you already tried this?
FileOutputStream fout = new FileOutputStream("YourPath", true);
The true value as the second parameter allows you to write in this file in append mode.
So you can serialize them individually and they will be serialized in the same file.

Serialize ArrayList<Objects> and back, preserving class

I am trying to send a collection of diffrent objects to server which accepts Json objects. Figuring out which is the optimal way to do this.
Plan A:
Serialize a collection of objects, like this:
ArrayList<Object> objects = new ArrayList<>();
objects.put(new Human("Johhny"));
objects.put(new Cannon(13));
objects.put(new Hamburger(1.3));
String json = Utils.getObjectMapper().writeValueAsString(objects);
Serialization works fine, and when I must deserialize, I receive array of Objects too (linked hash maps, to be precise). The problem is, I don't know which of these are Human, Cannon or Hamburger.
Is there any way to put object's class name into json, so when it's deserialized, the object mappers knows which class to use?
Plan B:
Parse each LinkedHashMap, manually determine it's true Class based on properties, and manually deserialize into object
Plan C:
Put each type of objects into diffrent collection with specific type.
Serialize three collections and combine them into one string with specific splitter
Deserialize back in reversed order.
The solution is:
Simply add mapper setting before writing to string: Utils.getObjectMapper().enableDefaultTyping(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT).writeValueAsString(objects);
Thanks to #dmitry-zvorygin!
Polymorphic (de)serialization is all you need -
https://github.com/FasterXML/jackson-docs/wiki/JacksonPolymorphicDeserialization
Just make sure you have base class for all the entities.

Jackson JSON serializer: enable or disable serialisation of certain field in runtime?

I have trivial object serialization via Jackson library:
ObjectMapper objectMapper = new ObjectMapper();
String jsonText = objectMapper.writeValueAsString(myComplexObject);
I have one field in myComplexObject that I sometimes do not want to edn up in serialized result. I know that I can avoid serialization completely if I declare a field in myComplexObject with #JsonIgnore, but I need that field sometimes present in the JSON. Can I achieve this effect? I can assign that field to NULL or to some other special value in case I do not need it.
Quite a few ways, from simple JSON Views to #JsonFilter, explained at "Every day Jackson usage, part 3: Filtering properties" (and its followup, "Advanced filtering with Jackson, Json Filters")

Object to Json including only fields not null

I have an object named Item with plenty of fields. I'm using a Java JSON (json-io) library to serialize its state in a File:
String json = JsonWriter.objectToJson(item);
// Write String to File
I'd like to make the json String less verbose so that it does not include null values (if possible also boolean which are false). Is it possible to do it somehow ?
Thanks
I can recommend using jackson mapper if possible.
Check out these two questions:
Jackson serialization: ignore empty values (or null)
How to tell Jackson to ignore a field during serialization if its value is null?

Parsing a json object which has many fields

I want to parse json from a server and place it into a class. I use json4s for this. The issue is that a json object contains too many fields, it's about 40-50 of them, some of them have the long names.
I wonder, what will be a sensible way to store all of them, will I have to create 40-50 fields in a class? Remember, some of them will have the long names, as I said earlier.
I use Scala, but a Java's approach might be similar to it, so I added a tag of Java also.
I don't know json4s but in Jersey with Jackson, for example, you can use a Map to hold the Json data or you can use a POJO with all those names.
Sometimes its better to have the names. It makes the code much easier to understand.
Sometimes its better to use a Map. For example, if the field names change from time to time.
If I recall it correctly, using pure Jackson you do something like this:
String jsonString = ....; // This is the string of JSON stuff
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory); // A Jackson class
Map<String,Object> data = mapper.readValue(jsonString, HashMap.class);
You can use a TypeReference to make it a little cleaner as regards the generics. Jackson docs tell more about it. There is also more here: StackOverflow: JSON to Map
There are generally two ways of parsing json to object
1) Parse json to object representation.
the other which might suit you as you mention that your object has too many fields is amap/hashtable, or you could just keep it as JObject, an get fields ehrn you need them

Categories

Resources