Importance of a JSONTokener in Java

Aishwarya Naglot
Updated on 12-May-2025 11:03:10

2K+ Views

If you have ever worked with JSON in Java, you might have used classes like JSONObject, JSONArray, JsonParser, etc. These classes are part of the org.json library. We use these classes to build JSON or parse JSON. But behind the scenes, these classes use a class called JSONTokener. In this article, we will learn about the JSONTokener class and its importance in Java. What is JSONTokener? The JSONTokener class is like a tool that reads a JSON string step by step, breaking it into smaller parts called tokens. It is like a helper that makes it easier to understand and ... Read More

Implement Custom JSON Serialization with Gson in Java

Aishwarya Naglot
Updated on 12-May-2025 10:38:49

1K+ Views

Custom JSON serialization Custom JSON Serialization means defining how your Java objects should be converted to JSON format. This is useful when you want to control the JSON output, such as changing field names, excluding certain fields, or formatting the data in a specific way. In this tutorial, we will create a custom serializer for a Java object using the Gson library. We will define a class Person with fields for name and age, and then create a custom serializer to control how this object is converted to JSON. To use Gson library, we need to add the Gson library ... Read More

Convert XML to JSON Array in Java

Aishwarya Naglot
Updated on 12-May-2025 10:31:12

9K+ Views

Xml is a markup language that is used to store or transport data. It uses tags to define the data. There are libraries also available for converting XML to JSON. Those are: org.json Jackson Gson Let's learn how to convert XML to JSON using various libraries. Converting XML to JSON using org.json For converting XML to JSON, we will use the org.json library. It is a simple library that is used for converting XML to JSON and vice versa. We can use the XML.toJSONObject() method to ... Read More

Write JSON String to File Using Gson Library in Java

Aishwarya Naglot
Updated on 12-May-2025 10:22:09

2K+ Views

Let's learn how to write a JSON string to a file using the Gson library in Java. If you are not familiar with JSON, you can refer to JSON Overview. Gson is a library that can be used to convert Java Objects to a JSON representation. To know more about Gson, refer to the Gson tutorial. GsonBuilder Class The primary class to use is Gson, which we can create by calling the new Gson(), and the GsonBuilder class can be used to create a Gson instance. We can write a JSON string to a file using the toJson() method of ... Read More

Implement Custom JSON De-Serialization with Gson in Java

Aishwarya Naglot
Updated on 12-May-2025 10:12:16

744 Views

Deserialization is the process of converting JSON data back into Java objects. Gson provides a simple way to do this using the fromJson() method. Custom JSON de-serialization with Gson Gson is a Java library developed by Google to convert Java objects into their JSON format and vice versa. Custom JSON is a way we can modify or extend the standard JSON format so that it can suit our specific needs. To use the Gson library, we need to add the Gson library to our project. If you are using Maven, add this to your pom.xml file: com.google.code.gson gson 2.8.9 ... Read More

List All Files in a Directory Recursively in Java

Aishwarya Naglot
Updated on 12-May-2025 10:04:59

5K+ Views

The given task is to list all the files from a specific directory, recursively, which means we need to retrieve all the files in the main directory and all the subdirectories as well. For example, if we have a directory structure like below - MyDirectory / \ / ... Read More

Serialize and Deserialize Generic Types Using Gson Library in Java

Aishwarya Naglot
Updated on 12-May-2025 09:15:57

2K+ Views

If a Java class is a generic type, and we are using it with the Gson library for JSON serialization and deserialization, the TypeToken class from com.google.gson.reflect package is used for preserving the generic type information at runtime. What are Generic Types in Java? Generic types in Java mean we don't set any method or class to a specific type, like int, String, etc. Instead, we use a type parameter (like T) to represent the type. This allows us to create classes and methods that can operate on objects of different types, and also provides compile-time safety at the same time. ... Read More

Java Stream findAny() with Examples

Aishwarya Naglot
Updated on 09-May-2025 18:39:31

1K+ Views

In this article, we will learn the findAny() method with examples in Java. The findAny() method in Java Streams is a tool that is used for fetching an arbitrary element from a stream. It provides a quick and easy way to retrieve any element without writing any conditions. This method returns a container that may or may not contain a non-null value, which is called an Optional object. What is the findAny() Method? The findAny() method retrieves any element from the stream, and the result is wrapped in an Optional object. If the stream is empty, the Optional object will ... Read More

C++ Program to Check Prime Number Using a Function

Nishu Kumari
Updated on 09-May-2025 16:05:32

7K+ Views

In this article, we will show you how to check whether a number is prime by creating a function in C++. A prime number is a number greater than 1 that has no divisors other than 1 and itself. For example, 2, 3, 5, 7, and 11 are prime numbers because they have only two divisors (1 and themselves), whereas 4, 6, 8, and 9 are not prime. Using a Function to Check Prime Number To check if a number is prime, we define a function that performs the check and returns true if the number ... Read More

C++ Program to Make a Simple Calculator Using Switch Case

Nishu Kumari
Updated on 09-May-2025 15:50:08

2K+ Views

In this article, we'll show you how to write a C++ program to create a simple calculator that can add, subtract, multiply, or divide using a switch statement. The calculator works by taking two numbers(operands) and an operator (+, -, *, /) from the user, and then it performs the chosen operation. Lets understand it better with an example: Suppose the user enters: a = 5 and b = 4 Then, based on the chosen operator: If the operator is +, the result is 5 + 4 = 9 If the operator is -, the result is 5 ... Read More

Advertisements