Found 206 Articles for JSON

How to define the naming conventions for JSON field names in Java?

raja
Updated on 14-Feb-2020 06:58:38

1K+ Views

The FieldNamingPolicy can be used to define a few standard naming conventions for JSON field names and it can be used in conjunction with GsonBuilder to configure a Gson instance to properly translate Java field names into the desired JSON field names. We can use the setFieldNamingPolicy() method of GsonBuilder to configure a specific naming policy strategy to an object's field during serialization and deserialization.Gson supports various field naming requirements with following field naming policiesFieldNamingPolicy.IDENTITY: It uses the exact same naming as the Java model when it serializes an object.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES: It modifies a Java field name from its camel-cased form to a lower case field name where ... Read More

How to exclude a field in Gson during serialization in Java?

raja
Updated on 06-Jul-2020 07:36:24

452 Views

The Gson library provided a simple approach to exclude fields from serialization using a transient modifier. If we make a field in a Java class as transient then Gson can ignore for both serialization and deserialization.Exampleimport com.google.gson.*; public class GsonTransientFieldTest {    public static void main(String[] args) {       Gson gson = new GsonBuilder().setPrettyPrinting().create();       Person p = new Person("Raja", "Ramesh", 28, 35000.00);       String jsonStr = gson.toJson(p);       System.out.println(jsonStr);    } } //Person class class Person {    private String firstName;    private transient String lastName;    private int age;    private transient double salary; ... Read More

Differences between fromJson() and toJson() methods of Gson in Java?

raja
Updated on 06-Jul-2020 07:38:10

4K+ Views

A Gson is a library for java and it can be used to generate a JSON. We can use the fromJson() method of Gson to parse JSON string into java object and use the toJson() method of Gson to convert Java objects into JSON string. There are two parameters in the fromJson() method, the first parameter is JSON String which we want to parse and the second parameter is Java class to parse JSON string. We can pass one parameter into the toJson() method is the Java object which we want to convert into a JSON string.Syntax for fromJson()public fromJson(java.lang.String json, java.lang.Class classOfT) throws ... Read More

How can we serialize an array of objects using flexjson in Java?

raja
Updated on 06-Jul-2020 07:08:44

327 Views

The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. We can also serialize an array of objects using the serialize() method of JSONSerializer class, this performs a shallow serialization of the target instance.Syntaxpublic String serialize(Object target)In the below program, we need to serialize an array of objects.Exampleimport flexjson.JSONSerializer; public class JsonSerializeArrayTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().prettyPrint(true);       Student stud1 = new Student("Adithya", "Sai", 28, "Hyderabad");       Student stud2 = new Student("Jai", "Dev", 30, "Chennai");       Student stud3 = new Student("Ravi", "Chandra", 35, ... Read More

How to deserialize a JSON into an existing object in Java?

raja
Updated on 06-Jul-2020 07:04:40

2K+ Views

The Flexjson is a lightweight java library for serializing and deserializing java beans, maps, arrays, and collections in JSON format. We can also deserialize a JSON string to an existing object using the deserializeInto() method of JSONDeserializer class, this method deserializes the given input into the existing object target. The values in the json input can overwrite values in the target object. This means if a value is included in JSON a new object can be created and set into the existing object.Syntaxpublic T deserializeInto(String input, T target)Exampleimport flexjson.JSONDeserializer; public class JsonDeserializeTest {    public static void main(String[] args) {       Employee emp = ... Read More

How to exclude a field from JSON using @Expose annotation in Java?

raja
Updated on 06-Jul-2020 06:52:49

2K+ Views

The Gson @Expose annotation can be used to mark a field to be exposed or not (included or not) for serialized or deserialized. The @Expose annotation can take two parameters and each parameter is a boolean which can take either the value true or false. In order to get GSON to react to the @Expose annotations we must create a Gson instance using the GsonBuilder class and need to call the excludeFieldsWithoutExposeAnnotation() method, it configures Gson to exclude all fields from consideration for serialization or deserialization that do not have the Expose annotation.Syntaxpublic GsonBuilder excludeFieldsWithoutExposeAnnotation()Exampleimport com.google.gson.*; import com.google.gson.annotations.*; public class JsonExcludeAnnotationTest {   ... Read More

How can we read & write a file using Gson streaming API in Java?

raja
Updated on 06-Jul-2020 06:24:19

804 Views

We can read and write a file using Gson streaming API and it is based on sequential read and write standard. The JsonWriter and JsonReader are the core classes built for streaming write and read in Streaming API. The JsonWriter writes a JSON encoded value to a stream, one token at a time. The stream includes both literal values (strings, numbers, booleans, and nulls) as well as begin and end delimiters of objects and arrays and JsonReader reads a JSON encoded value as a stream of tokens. This stream includes both literal values (strings, numbers, booleans, and nulls) as well as the begin and end delimiters of objects and arrays. The ... Read More

How to parse a JSON to Gson Tree Model in Java?

raja
Updated on 06-Jul-2020 06:04:59

1K+ Views

The Gson library can be used to parse a JSON String into a Tree Model. We can use the JsonParser to parse the JSON string into a Tree Model of type JsonElement. The getAsJsonObject() method of JsonElement can be used to get the element as JsonObject and getAsJsonArray() method of JsonElement can be used to get the element as JsonArray.Syntaxpublic JsonObject getAsJsonObject() public JsonArray getAsJsonArray()Exampleimport java.util.List; import com.google.gson.*; public class JsonTreeModelTest {    public static void main(String args[]){       String jsonStr = "{\"name\":\"Adithya\", \"age\":20, \"year of passout\":2005, \"subjects\": [\"MATHEMATICS\", \"PHYSICS\", \"CHEMISTRY\"]}";       JsonParser jsonParser = new JsonParser();       JsonElement jsonElement = jsonParser.parse(jsonStr); ... Read More

How can we change a field name in JSON using Jackson in Java?

raja
Updated on 06-Jul-2020 06:06:00

4K+ Views

The Jackson Annotation @JsonProperty is used on a property or method during serialization or deserialization of JSON. It takes an optional ‘name’ parameter which is useful in case the property name is different than ‘key’ name in JSON. By default, if the key name matches the property name, value is mapped to property value.In the below example, we can change a field name in JSON using @JsonProperty annotation.Exampleimport java.io.IOException; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.annotation.JsonProperty; public class JsonPropertyAnnotationTest {    public static void main(String[] args) throws IOException {       ObjectMapper mapper = new ObjectMapper();       mapper.enable(SerializationFeature.INDENT_OUTPUT);       User user = new ... Read More

How to ignore the null and empty fields using the Jackson library in Java?

raja
Updated on 06-Jul-2020 05:57:50

1K+ Views

The Jackson is a library for Java and it has very powerful data binding capabilities and provides a framework to serialize custom java objects to JSON and deserialize JSON back to Java object. The Jackson library provides @JsonInclude annotation that controls the serialization of a class as a whole or its individual fields based on their values during serialization.The @JsonInclude annotation contains below two valuesInclude.NON_NULL: Indicates that only properties with not null values will be included in JSON.Include.NON_EMPTY: Indicates that only properties that are not empty will be included in JSONExampleimport com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; public class IgnoreNullAndEmptyFieldTest ... Read More

Advertisements