Found 206 Articles for JSON

How to convert a List to JSON array using the Jackson library in Java?

raja
Updated on 06-Jul-2020 12:43:38

10K+ Views

The ObjectMapper class is the most important class in the Jackson API that provides readValue() and writeValue() methods to transform JSON to Java Object and Java Object to JSON. We can convert a List to JSON array using the writeValueAsString() method of ObjectMapper class and this method can be used to serialize any Java value as a String.Syntaxpublic String writeValueAsString(Object value) throws JsonProcessingExceptionExampleimport java.util.*; import com.fasterxml.jackson.databind.*; public class ListToJSONArrayTest {    public static void main(String args[]) {       List list = new ArrayList();       list.add("JAVA");       list.add("PYTHON");       list.add("SCALA");       list.add(".NET");   ... Read More

How to define alternate names to a field using Jackson in Java?

raja
Updated on 17-Feb-2020 07:20:53

4K+ Views

The @JsonAlias annotation can define one or more alternate names for the attributes accepted during the deserialization, setting the JSON data to a Java object. But when serializing, i.e. getting JSON from a Java object, only the actual logical property name is used instead of the alias.Syntax@Target(value={ANNOTATION_TYPE, FIELD, METHOD, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonAliasExampleimport com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import java.io.*; public class ObjectToJsonTest {    public static void main(String[] args) throws JsonProcessingException {       ObjectMapper mapper = new ObjectMapper();       Technology tech = new Technology("Java", "Oracle");       Employee emp = new Employee(110, "Raja", tech);   ... Read More

How can we map multiple date formats using Jackson in Java?

raja
Updated on 06-Jul-2020 12:33:55

2K+ Views

A Jackson is a Java-based library and it can be useful to convert Java objects to JSON and JSON to Java Object. We can map the multiple date formats in the Jackson library using @JsonFormat annotation, it is a general-purpose annotation used for configuring details of how values of properties are to be serialized. The @JsonFormat has three important fields: shape, pattern,  and timezone. The shape field can define structure to use for serialization (JsonFormat.Shape.NUMBER and JsonFormat.Shape.STRING), the pattern field can be used in serialization and deserialization. For date, the pattern contains SimpleDateFormat compatible definition and finally, the timezone field can be used in serialization, default ... Read More

How can we convert a JSON array to a list using Jackson in Java?

raja
Updated on 06-Jul-2020 12:30:14

8K+ Views

A Jackson is a Java-based library and it can be useful to convert Java objects to JSON and JSON to Java Object. A Jackson API is faster than other API, needs less memory area and is good for the large objects. We can convert a JSON array to a list using the ObjectMapper class. It has a useful method readValue() which takes a JSON string and converts it to the object class specified in the second argument.Exampleimport java.util.*; import com.fasterxml.jackson.databind.*; public class JSONArrayToListTest1 {    public static void main(String args[]) {       String jsonStr = "[\"INDIA\", \"AUSTRALIA\", \"ENGLAND\", \"SOUTH AFRICA\", \"WEST ... Read More

How can we serialize a list of objects using flexjson in Java?

raja
Updated on 06-Jul-2020 12:26:47

1K+ Views

The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. We can serialize a list of objects using the serialize() method of JSONSerializer class. This method can perform a shallow serialization of the target instance. We need to pass a list of objects of List type as an argument to the serialize() method.Syntaxpublic String serialize(Object target)Exampleimport flexjson.JSONSerializer; import java.util.*; public class JsonSerializeListTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print JSON       Student s1 = new Student("Raja", "Ramesh", 28, "Hyderabad");       Student s2 = new Student("Suresh", "Kumar", 30, "Chennai");   ... Read More

How can we create a JSON using JsonGenerator in Java?

raja
Updated on 06-Jul-2020 12:19:03

5K+ Views

The JsonGenerator is a base class that defines public API for writing JSON content. The instances are created using factory methods of a JsonFactory instance. Once we can get the JsonGenerator from factory instance then use the writeStartObject() for writing starting marker of a JSON Object value,  writeFieldName() method for writing a field name, writeString() method for outputting a String value, writeStartArray() method for writing starting marker of a Array value and writeEndObject() method for writing closing marker of a JSON Object value.Syntaxpublic abstract class JsonGenerator extends Object implements Closeable, Flushable, VersionedExampleimport java.io.*; import com.fasterxml.jackson.core.*; public class JsonGeneratorTest {      public static void main(String ... Read More

How to create a JSON using Jackson Tree Model in Java?

raja
Updated on 06-Jul-2020 12:20:51

959 Views

In the Jackson library, we can use the Tree Model to represent the JSON structure and perform the CRUD operations via JsonNode. This Jackson Tree Model is useful, especially in cases where a JSON structure does not map to Java classes. We can create a JSON in the Jackson library using the JsonNodeFactory, it can specify the methods for getting access to Node instances as well as the basic implementation of the methods. We can use the set() and put() methods of ObjectNode class to populate the data.Syntaxpublic class JsonNodeFactory extends Object implements SerializableExampleimport java.io.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.node.*; public class JacksonTreeModelTest {    public static ... Read More

When can we call @JsonAnyGetter and @JsonAnySetter annotations in Java?

raja
Updated on 06-Jul-2020 12:14:04

1K+ Views

The @JsonAnyGetter annotation enables to use a Map as a container for properties that we want to serialize to JSON and @JsonAnySetter annotation instructs Jackson to call the same setter method for all unrecognized fields in the JSON object, which means that all fields that are not already mapped to a property or setter method in the Java object.Syntaxpublic @interface JsonAnyGetter public @interface JsonAnyGetterExampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.annotation.*; public class JsonAnyGetterAndJsonAnySetterTest {    public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {       Employee emp1 = new Employee();       emp1.setFirstName("Adithya");       emp1.setLastName("Sai"); ... Read More

How can we ignore the fields during JSON serialization in Java?

raja
Updated on 06-Jul-2020 12:06:01

12K+ Views

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.Syntaxpublic @interface JsonIgnoreExampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.annotation.*; public class JsonIgnoreAnnotationTest {    public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {       Employee emp = new Employee();       emp.setFirstName("Raja");       emp.setLastName("Ramesh");       emp.setEmpId(120);       emp.getTechnologies().add("Java");       emp.getTechnologies().add("Scala");       emp.getTechnologies().add("Python");       ... Read More

How to serialize the order of properties using the Jackson library in Java?

raja
Updated on 06-Jul-2020 12:01:48

3K+ Views

The @JsonPropertyOrder is an annotation to be used at the class-level. It takes as property a list of fields that defines the order in which fields can appear in the string resulting from the object JSON serialization. The properties included in the annotation declaration can be serialized first(in defined order), followed by any properties not included in the definition.Syntaxpublic @interface JsonPropertyOrderExampleimport com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import java.util.*; import java.io.*; public class JsonPropertyOrderTest {    public static void main(String args[]) throws JsonGenerationException, JsonMappingException,        IOException {       Employee emp = new Employee();       emp.setFirstName("Adithya");     ... Read More

Advertisements