Raja has Published 760 Articles

How to serialize and deserialize a JSON using the ExclusionStrategy interface in Java?

raja

raja

Updated on 08-Jul-2020 06:48:57

601 Views

The ExclusionStrategy interface can be used to exclude any field during serialization and deserialization. We can provide a custom implementation of the ExclusionStrategy interface and need to register it with GsonBuilder using the setExclusionStrategies() method. It configures Gson to apply a set of exclusion strategies during serialization and deserialization.Syntaxpublic GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies)Exampleimport com.google.gson.*; import ... Read More

How can we update an existing JSON data using javax.json API in Java?

raja

raja

Updated on 08-Jul-2020 06:48:13

6K+ Views

The JsonBuilderFactory interface is a factory to create JsonObjectBuilder instance and JsonObjectBuilder is a builder for creating JsonObject models from scratch. This interface initializes an empty JSON object model and provides methods to add name/value pairs to the object model and to return the resulting object. We can create a JsonObjectBuilder instance that can be used ... Read More

JSON Schema Support using Jackson in Java?

raja

raja

Updated on 08-Jul-2020 06:46:35

4K+ Views

JSON Schema is a specification for JSON based format for defining the structure of JSON data. The JsonSchema class can provide a contract for what JSON data is required for a given application and how to interact with it. The JsonSchema can define validation, documentation, hyperlink navigation, and interaction control of JSON ... Read More

How to convert Java array or ArrayList to JsonArray using Gson in Java?

raja

raja

Updated on 08-Jul-2020 06:20:07

6K+ Views

The Java Arrays are objects which store multiple variables of the same type, it holds primitive types and object references and an ArrayList can represent a resizable list of objects. We can add, remove, find, sort and replace elements using the list. A JsonArray can parse text from a string to produce a ... Read More

Differences between org.simple.json and org.json libraries in Java?

raja

raja

Updated on 08-Jul-2020 06:19:28

2K+ Views

The org.json.simple library allows us to read and write JSON data in Java. In other words, we can encode and decode the JSON object. The org.json.simple package contains important classes like JSONValue, JSONObject, JSONArray, JsonString and JsonNumber. We need to install the json-simple.jar file to execute a JSON program whereas org.json library has classes to ... Read More

How to resolve "Expected BEGIN_OBJECT but was BEGIN_ARRAY" using Gson in Java?

raja

raja

Updated on 08-Jul-2020 05:49:52

16K+ Views

While deserializing, a Gson can expect a JSON object but it can find a JSON array. Since it can't convert from one to the other, it can throw an error as "JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY" at the runtime.Exampleimport com.google.gson.Gson; public class GsonErrorTest {    public static void ... Read More

How to serialize a JSON object with JsonWriter using Object Model in Java?

raja

raja

Updated on 08-Jul-2020 05:40:56

2K+ Views

The javax.json.JsonWriter interface can write a JSON object or array structure to an output source. The class javax.json.JsonWriterFactory contains methods to create JsonWriter instances. A factory instance can be used to create multiple writer instances with the same configuration. We can create writers from output source using the static method createWriter() of javax.json.Json class.Syntaxpublic static JsonWriter createWriter(Writer ... Read More

How to handle the errors generated while deserializing a JSON in Java?

raja

raja

Updated on 07-Jul-2020 13:18:11

2K+ Views

The DeserializationProblemHandler class can be registered to get called when a potentially recoverable problem is encountered during the deserialization process. We can handle the errors generated while deserializing the JSON by implementing the handleUnknownProperty() method of DeserializationProblemHandler class.Syntaxpublic boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer deserializer, Object beanOrClass, String propertyName) throws IOExceptionExampleimport java.io.*; ... Read More

How to merge two JSON strings in an order using JsonParserSequence in Java?

raja

raja

Updated on 07-Jul-2020 13:06:13

680 Views

The JsonParserSequence is a helper class that can be used to create a parser containing two sub-parsers placed in a particular sequence. We can create a sequence using the static method createFlattened() of the JsonParserSequence class.Syntaxpublic static JsonParserSequence createFlattened(JsonParser first, JsonParser second)Exampleimport java.io.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.core.util.*; public class JsonParserSequenceTest {    public static void main(String[] ... Read More

How to map the JSON data with Jackson Object Model in Java?

raja

raja

Updated on 07-Jul-2020 13:04:14

3K+ Views

The ObjectMapper class provides functionality for converting between Java objects and matching JSON constructs. We can achieve mapping of JSON data represented by an Object Model to a particular Java object using a tree-like data structure that reads and stores the entire JSON content in memory. In the first step, read ... Read More

Advertisements