Found 2616 Articles for Java

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

Pretty print JSON using the flexjson library in Java?

raja
Updated on 06-Jul-2020 05:53:33

232 Views

The Flexjson is a lightweight Java library for serializing and de-serializing java beans, maps, arrays, and collections in a JSON format. A JSONSerializer is the main class for performing serialization of Java objects to JSON and by default performs a shallow serialization. We can pretty-print JSON using the prettyPrint(boolean prettyPrint) method of JSONSerializer class.Syntaxpublic JSONSerializer prettyPrint(boolean prettyPrint)In the below program, Pretty print JSON using flexjson libraryExampleimport flexjson.*; public class PrettyPrintJSONTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print       Employee emp = new Employee("Vamsi", "105", "Python Developer", "Python", "Pune");       String jsonStr = ... Read More

How to deserialize a JSON to Java object using the flexjson in Java?

raja
Updated on 04-Jul-2020 13:01:23

4K+ Views

The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format allowing both deep and shallow copies of objects. In order to run a Java program with flexjon, we need to import a flexjson package. We can deserialize a JSON to Java object using the deserialize() method of JSONDeserializer class, it takes as input a json string and produces a static typed object graph from that json representation. By default, it uses the class property in the json data in order to map the untyped generic json data into a specific Java type.Syntaxpublic T deserialize(String input)In the below program, deserialize ... Read More

Custom instance creator using Gson in Java?

raja
Updated on 04-Jul-2020 12:48:43

2K+ Views

While parsing JSON String to or from Java object, By default Gson try to create an instance of Java class by calling the default constructor. In the case of Java class doesn’t contain default constructor or we want to do some initial configuration while creating Java objects, we need to create and register our own instance creator.We can create a custom instance creator in Gson using the InstanceCreator interface and need to implement the createInstance(Type type) method.SyntaxT createInstance(Type type)Exampleimport java.lang.reflect.Type; import com.google.gson.*; public class CustomInstanceCreatorTest {    public static void main(String args[]) {       GsonBuilder gsonBuilder = new GsonBuilder();   ... Read More

EnumMap class in Java

AmitDiwan
Updated on 26-Sep-2019 13:27:04

134 Views

The java.util.EnumMap class is a specialized Map implementation for use with enum keys. Following are the important points about EnumMap −All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created.Enum maps are maintained in the natural order of their keys.EnumMap is not synchronized. If multiple threads access an enum map concurrently, and at least one of the threads modifies the map, it should be synchronized externally.Following are the constructors of the EnumMap class −Sr.NoConstructor & Description1EnumMap(Class keyType)This constructor creates an empty enum map with the specified ... Read More

Compare two Strings in Java

AmitDiwan
Updated on 26-Sep-2019 13:23:44

292 Views

Compare two strings using compareTo() method in Java. The syntax is as follows −int compareTo(Object o)Here, o is the object to be compared.The return value is 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.ExampleLet us now see an example − Live Demopublic class Demo {    public static void main(String args[]) {       String str1 = "Strings are immutable";       ... Read More

Java streams counting() method with examples

AmitDiwan
Updated on 26-Sep-2019 13:13:13

245 Views

Count the number of elements in the stream using the Java streams counting() method. Following is an example to implement the Java Streams counting() method −Example Live Demoimport java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Stream stream = Stream.of("Kevin", "Jofra", "Tom", "Chris", "Liam");       // count       long count = stream.collect(Collectors.counting());       System.out.println("Number of elements in the stream = "+count);    } }OutputNumber of elements in the stream = 5Let us see another example wherein we have stream of integer elements −Example Live Demoimport ... Read More

Java Stream findAny() with examples

AmitDiwan
Updated on 26-Sep-2019 13:10:57

766 Views

The findAny() method of the Java Stream returns an Optional for some element of the stream or an empty Optional if the stream is empty. Here, Optional is a container object which may or may not contain a non-null value.Following is an example to implement the findAny() method in Java −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args){       List list = Arrays.asList(10, 20, 30, 40, 50);       Optional res = list.stream().findAny();       if (res.isPresent()) {          System.out.println(res.get());       } else {     ... Read More

Convert an Iterator to a List in Java

AmitDiwan
Updated on 26-Sep-2019 13:08:04

331 Views

Let’s say the following is our Iterator with Integer values −Iterator iterator = Arrays.asList(50, 100, 200, 300, 400, 500, 1000).iterator();Now, convert this Iterator to a List −List myList = new ArrayList(); iterator.forEachRemaining(myList::add);ExampleFollowing is the program to convert an Iterator to a List in Java − Live Demoimport java.util.*; public class Demo {    public static void main(String[] args){       Iterator iterator = Arrays.asList(50, 100, 200, 300, 400, 500, 1000).iterator();       List myList = new ArrayList();       iterator.forEachRemaining(myList::add);       System.out.println("Iterator to list = "+myList);    } }OutputIterator to list = [50, 100, 200, 300, 400, 500, 1000]

Advertisements