Pretty printing JSON is a process of formatting our JSON data to make it more readable and good-looking.The prettyPrint() Method of Flexjson We will be using the Flexjson library to pretty print JSON in Java. The Flexjson library is a lightweight library that is used for serializing as well as deserializing Java objects to and from JSON. A JSONSerializer is the main class for performing the 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. Syntax of prettyPrint() The syntax of the prettyPrint() method ... Read More
The Jackson is a library for Java and it has very powerful data binding capabilities and it also provides a framework to serialize custom java objects to JSON and deserialize JSON back to Java object. Ignoring Null and Empty Fields Using Jackson The Jackson library provides @JsonInclude annotation, which 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 values: Include.NON_NULL: Indicates that only properties with not null values will be included in JSON. Include.NON_EMPTY: Indicates ... Read More
Sometimes, we may need to parse a JSON into a Gson tree model in Java. For example, we may need to parse a JSON string into a tree model when we want to manipulate the JSON data in a more easy way.The getAsJsonObject() Method The Gson library is used to parse a JSON String into a Tree Model. We will be using the JsonParser to parse the JSON string into a Tree Model of type JsonElement. We will be using the getAsJsonObject() method of JsonElement for getting the element as JsonObject, and the other is getAsJsonArray(), which is a method ... Read More
When we have a JSON object and want to change a specific field name for some reason, we can use the Jackson library in Java.The Jackson Annotation @JsonProperty The Jackson Annotation @JsonProperty is used on a property or method during the serialization or deserialization of JSON. It takes an optional 'name' parameter, which is useful in case the property name is different from the key name in JSON. By default, if the key name matches the property name, the value is mapped to the property value. If the key name is different, we can use the @JsonProperty annotation to ... Read More
Sometimes we need to exclude certain fields from the JSON output. For example, if we have a field that contains sensitive information, we may not want to include it in the JSON representation of the object. In such cases, we can use the Expose annotation provided by Gson. The Gson @Expose annotation The Gson @Expose annotation can be used to specify whether a field isto be exposed or not (included or not). The @Expose annotation can take two parameters, and each parameter is a boolean which can take either the value true or false. The two parameters are: ... Read More
We can read and write a file using Gson streaming API, and it is based on the sequential read and write standard. The JsonWriter and JsonReader are the core classes built for streaming write and read in Streaming API. Reading & Writing a File using Gson 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. The tokens are traversed in depth-first order, ... Read More
An Iterator is used for cycling through a Collection of objects. Whereas a List is an Interface which is used for storing an ordered collection of elements. Both classes are part of the Java Collections Framework.Converting an Iterator to a List in Java Sometimes we need to convert an Iterator to a collection. In this article, we will learn how to convert an Iterator to a List in Java using multiple ways. Those are: Using a while loop Using Iterator.forEachRemaining() Using Stream API Let's understand each of them in ... Read More
What is an Iterable?Iterable is an Interface that is used for iterating over a Collection of objects. It is part of the Java Collections Framework. The Iterable interface is implemented by all the collection classes in Java. For example, ArrayList, HashSet, etc. The Iterable interface has a method named iterator() which returns an Iterator object.What is a Stream? Stream is an Interface that is used for processing sequences of elements. It is part of the Java Collections Framework. The Stream interface is a newly added abstraction in Java 8. It is mainly used for sequential operations on collections, for example, ... Read More
In Java, we can store objects within other objects. A collection is an object that stores other objects. Some examples of collections are ArrayList, HashSet. Iterable is an Interface that is used for iterating over a Collection of objects. It is part of the Java Collections Framework. The Iterable interface is implemented by all the collection classes in Java. The Iterable interface has a method named iterator() which returns an Iterator object. In this article, we will learn how to convert an Iterable to a Collection in Java using multiple ways. Those are - Using for-each ... Read More
In this article, we will learn to create a JPopupMenu with a submenu in Java. In general, we can add the menu items to a JPopupMenu and also add a submenu to JPopupMenu by adding the menu items to the submenu first, then adding it to JPopupMenu. What is a JPopupMenu? A JPopupMenu is a subclass of JComponent class and it can appear anywhere on the screen when a right mouse button is clicked. In order to create a popup menu, we can use the JPopupMenu class. Syntax The following is the syntax for JPopupMenu initialization: JPopupMenu menu = new ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP