Programming Articles - Page 2388 of 3366

How to rename the properties of JSON using Gson in Java?

raja
Updated on 06-Jul-2020 13:17:10

4K+ Views

The Gson @SerializedName annotation can be serialized to a JSON with the provided name value as its field name. This annotation can override any FieldNamingPolicy including the default field naming policy that may have been set on the Gson instance. A different naming policy can set using the GsonBuilder class.Syntax@Retention(value=RUNTIME) @Target(value={FIELD, METHOD}) public @interface SerializedNameExampleimport com.google.gson.annotations.*; import com.google.gson.*; public class SerializedNameAnnotationTest {    public static void main(String args[]) {       Employee emp = new Employee("Rahul", "Dev", 30, "Nagpur");       Gson gson = new GsonBuilder().setPrettyPrinting().create(); // pretty print       String jsonStr = gson.toJson(emp);       System.out.println(jsonStr);    } ... Read More

How to ignore the multiple properties of a JSON object in Java?

raja
Updated on 06-Jul-2020 13:18:16

4K+ Views

The @JsonIgnoreProperties Jackson annotation can be used to specify a list of properties or fields of a class to ignore. The @JsonIgnoreProperties annotation can be placed above the class declaration instead of above the individual properties or fields to ignore.Syntax@Target(value={ANNOTATION_TYPE, TYPE, METHOD, CONSTRUCTOR, FIELD}) @Retention(value=RUNTIME) public @interface JsonIgnorePropertiesExampleimport java.io.*; import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; public class JsonIgnorePropertiesTest {    public static void main(String[] args) throws IOException {       Customer customer = new Customer("120", "Ravi", "Hyderabad");       System.out.println(customer);       ObjectMapper mapper = new ObjectMapper();       String jsonString = mapper.writeValueAsString(customer);       System.out.println("JSON: " + jsonString);     ... Read More

Split the array into equal sum parts according to given conditions in C++

Arnab Chakraborty
Updated on 17-Oct-2019 13:36:42

842 Views

Here we will see one problem. Suppose one array arr is given. We have to check whether the array can be split into two parts, such that −Sub of both sub-arrays will be the sameAll elements, which are multiple of 5, will be in the same groupAll elements which are multiple of 3, but not multiple of 5, will be in the same groupAll other elements will be in other groups.Suppose the array elements are {1, 4, 3}, then this can be split, because the sum of {1, 3} is the same as the sum of {4}, and the groups ... Read More

How to serialize a JSON string to an Output Handler in Java?

raja
Updated on 06-Jul-2020 13:11:16

635 Views

The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. A JSONSerializer is the main class for performing serialization of Java objects to JSON. We can serialize a JSON string to an Output Handler using the WriterOutputHandler class and it implements the OutputHandler interface.Syntaxpublic class WriterOutputHandler extends Object implements OutputHandlerExampleimport java.io.*; import flexjson.JSONSerializer; import flexjson.OutputHandler; import flexjson.WriterOutputHandler; public class JsonOutputHandlerTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print JSON       Employee emp = new Employee("Raja", "Ramesh", 28, "Hyderabad");       OutputHandler out = new WriterOutputHandler(new ... Read More

How to ignore a field of JSON object using the Jackson library in Java?

raja
Updated on 06-Jul-2020 13:12:32

9K+ Views

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON. We can use the readValue() and writeValueAsString() methods of ObjectMapper class to read a JSON to Java Object and to write a Java object to JSON.Syntax@Target(value={ANNOTATION_TYPE, METHOD, CONSTRUCTOR, FIELD}) @Retention(value=RUNTIME) public @interface JsonIgnoreExampleimport java.io.*; import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; public class JsonIgnoreTest {    public static void main(String[] args) throws IOException {       Customer customer = new Customer("110", "Surya Kiran", "Chennai");       System.out.println(customer); ... Read More

Area of a polygon with given n ordered vertices in C++

Aman Kumar
Updated on 29-Jul-2025 14:39:53

1K+ Views

A polygon is a closed two-dimensional shape formed by connecting three or more straight lines end-to-end. These lines form sides, and their connection points are called vertices. When the vertices of the polygon are given in a specific order either clockwise or counter-clockwise, we can calculate the area of the polygon using a mathematical formula known as the Shoelace Formula or Surveyor’s Formula. You are given the coordinates of a polygon with n vertices. The vertices are provided in an ordered manner, meaning they are listed either in clockwise or anticlockwise order starting from the first vertex to the last. ... Read More

Area of a n-sided regular polygon with given side length in C++

Aman Kumar
Updated on 29-Jul-2025 14:47:49

1K+ Views

A regular polygon with n sides is a two-dimensional shape with n equal-length sides and n equal interior angles. It is both equilateral and equiangular (all sides are equal and all angles are equal). In the above diagram, we see that the polygon can be divided into n equal triangles. For one of the triangles, the complete angle at the center can be divided into = 2pi/n So, angle t = pi/n Now, tan(t) = a/2*h So, h = a/(2*tan(t)) Area of ... Read More

Alternative Sorting in C++

sudhir sharma
Updated on 16-Oct-2019 07:50:14

817 Views

Sorting the elements of an integer array in such a way that the first element is the maximum of the array and the second element of the sorted array is the minimum, the third one is the second minimum, the fourth one is the second maximum of the array and goes on.Let’s take an example to understand the concept better, Input : 4 1 8 2 9 3 7 Output : 9 1 8 2 7 3 4 Explanation : The elements in a sorted way is 1 2 3 4 7 8 9. Now, let’s create it in ... Read More

Alternate sorting of Linked list in C++

sudhir sharma
Updated on 16-Oct-2019 07:46:07

355 Views

A linked list is a linear data structure that stores elements and also stores a pointer to the next data node.In this problem on the sorting of a linked list, the alternate sort means sorting in such a way that the 1st node contains data with the minimum value, the 2nd node contains data with maximum value, 3rd with the next minimum (second minimum value) and so on. This pattern of alternate maxima and minimas is created in alternate sorting of linked lists.Let’s take an example to understand the problem better −Input : 3 > 4 > 21 >67 > ... Read More

Alternate Odd and Even Nodes in a Singly Linked List in C++

Nishu Kumari
Updated on 29-Jul-2025 12:57:44

375 Views

Given a singly linked list, we need to rearrange its nodes so that even and odd numbers come one after the other alternatively. If the list starts with an even number, the next should be odd, then even, and so on. Similarly, if it begins with an odd number, the next should be even, then odd, and so on. Let's look at some example scenarios to understand the concept better. Scenario 1 Input: 45 -> 21 -> 2 -> 213 -> 3 -> 34 -> 78 -> 12 Output: 45 -> 2 -> 21 -> 34 -> 213 -> 78 ... Read More

Advertisements