Programming Articles - Page 2407 of 3366

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

Aishwarya Naglot
Updated on 13-May-2025 16:23:07

5K+ Views

Gson is a Java library which is developed by Google and used to convert Java objects into JSON and vice versa. It is mostly used in applications where data can be exchanged in JSON format. There are two useful methods in Gson that we are going to discuss in this article. These methods are fromJson() and toJson(). The fromJson() Method The fromJson() method is used to convert a JSON string into a Java object. It takes two parameters: the JSON string and the class type of the object you want to create. Here is the syntax of the fromJson() method: ... Read More

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

Aishwarya Naglot
Updated on 13-May-2025 16:28:51

465 Views

Serialization is the process where we convert an object into a format that can be easily stored or transmitted and later reconstructed. The given task is to serialize an array of objects using Flexjson in Java. Serializing an Array of Objects Using Flexjson 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 the JSONDeserializer class, this method deserializes the given input into the existing object target. The values in the JSON input can overwrite ... Read More

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

Aishwarya Naglot
Updated on 13-May-2025 16:33:52

3K+ Views

Deserialization is the process of converting a JSON string into a Java object. In this example, we will learn how to deserialize a JSON string into an existing object in Java. There are libraries available in Java that can help us to deserialize a JSON string into an existing object. Some of the popular libraries are: Using Gson Using Jackson Using Flexjson Let's learn how to use each of these libraries to deserialize a JSON string into an existing object in Java. Deserializing JSON Object Using ... Read More

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

Aishwarya Naglot
Updated on 12-May-2025 13:09:12

3K+ Views

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

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

Aishwarya Naglot
Updated on 12-May-2025 13:06:24

1K+ Views

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

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

Aishwarya Naglot
Updated on 12-May-2025 14:11:05

2K+ Views

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

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

Aishwarya Naglot
Updated on 12-May-2025 14:02:30

4K+ Views

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

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

Aishwarya Naglot
Updated on 12-May-2025 14:17:47

2K+ Views

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

Pretty print JSON using the flexjson library in Java?

Aishwarya Naglot
Updated on 12-May-2025 14:22:00

349 Views

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

Unit Testing in Python Program using Unittest

Pavitra
Updated on 27-Sep-2019 11:30:13

208 Views

In this article, we will learn about the fundamentals of software testing by the help of unittest module available in Python 3.x. Or earlier. It allows automation, sharing of the setup and exit code for tests, and independent tests for every framework.In unit test, we use a wide variety of object oriented concepts. We will be discussing some majorly used concepts here.Testcase − It is response specific base class in accordance with a given set of inputs. We use base class of unittest i.e. “TestCase” to implement this operation.Testsuite − It is used to club test cases together and execute ... Read More

Advertisements