When to Use @ConstructorProperties Annotation with Jackson in Java

Aishwarya Naglot
Updated on 09-Jul-2020 07:19:56

2K+ Views

Jackson is a library that is used for converting Java objects to JSON and vice versa. It provides various annotations to customize the serialization and deserialization process. One such annotation is @ConstructorProperties, which is used to specify the properties of a constructor for deserialization. The @ConstructorProperties annotation is from java.beans package, used to deserialize JSON to a Java object via the annotated constructor. If we use this annotation rather than annotating each parameter in the constructor, we can provide an array with the property names for each of the constructor parameters. Let's learn when to use the @ConstructorProperties annotation with ... Read More

Ways to Read Input from Console in Java

AmitDiwan
Updated on 09-Jul-2020 06:50:11

5K+ Views

Let us see some ways to read input from console in Java −Exampleimport java.util.Scanner; public class Demo{    public static void main(String args[]){       Scanner my_scan = new Scanner(System.in);       String my_str = my_scan.nextLine();       System.out.println("The string is "+my_str);       int my_val = my_scan.nextInt();       System.out.println("The integer is "+my_val);       float my_float = my_scan.nextFloat();       System.out.println("The float value is "+my_float);    } }OutputThe string is Joe The integer is 56 The float value is 78.99A class named Demo contains the main function. An instance of the ... Read More

Using Variables in JShell of Java 9

AmitDiwan
Updated on 09-Jul-2020 06:47:43

506 Views

In JShell 9, variables can be declared during a session. Once the user has logged into the session, they can declare a variable as follows −jshell> int val = 56 ;Italics indicate the terminal, once the user has logged in to their session.The above line would print the below output. The semicolon in the above line is optional, and it will run fine without the semicolon also.Outputval = = > 56When an integer value is defined by assigning it to a variable name on the JShell, and it is executed, by pressing ‘Enter’ key, it is displayed on the next ... Read More

Using Underscore in Numeric Literals in Java

AmitDiwan
Updated on 09-Jul-2020 06:45:34

188 Views

Followig is the code showing how to use underscore in numeric literals in Java −Example Live Demopublic class Demo{    public static void main (String[] args) throws java.lang.Exception{       int my_num_1 = 6_78_00_120;       System.out.println("The number is : " + my_num_1);       long my_num_2 = 2_00_11_001;       System.out.println("The number is : " + my_num_2);       float my_num_3 = 4.01_981F;       System.out.println("The number is : " + my_num_3);       double my_num_4 = 12.89_46_061;       System.out.println("The number is : " + my_num_4);    } }OutputThe number is ... Read More

Using Predefined Class Name as Class or Variable Name in Java

AmitDiwan
Updated on 09-Jul-2020 06:43:55

1K+ Views

Using a predefined class name as a class nameLet us see an example −Example Live Demopublic class Number{    public static void main (String[] args){       System.out.println("Pre-defined class name can be used as a class name");    } }OutputPre-defined class name can be used as a class nameThe class Number has a main function that displays a message when it is executed. The main function takes string values as arguments.Using a predefined class name as a variable nameLet us see an example −Example Live Demopublic class String{    public static void main (java.lang.String[] args){       System.out.println("Pre-defined class name ... Read More

Importance of @JsonUnwrapped Annotation Using Jackson in Java

raja
Updated on 09-Jul-2020 06:43:52

993 Views

The @JsonUnwrapped annotation can be used to unwrap values during the serialization and deserialization process. It helps to render the values of a composed class as if it belongs to the parent class.Syntax@Target(value={ANNOTATION_TYPE, FIELD, METHOD, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonUnwrappedExampleimport com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.JsonProcessingException; public class JsonUnwrappedAnnotationTest {    public static void main(String args[]) throws JsonProcessingException {       ObjectMapper mapper = new ObjectMapper();       String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Employee());       System.out.println(jsonString);    } } class Employee {    public int empId = 110;    public String empName = "Raja Ramesh";    @JsonUnwrapped   ... Read More

Sort User-Defined Objects Using TreeMap in Java

AmitDiwan
Updated on 09-Jul-2020 06:42:38

212 Views

To sort user-defined object in Java, the code is as follows −Example Live Demoimport java.io.*; import java.util.*; public class Demo{    static void sort_objects(String my_data){       String[] my_vals = my_data.split(" ");       Map my_map = new TreeMap();       for (int i = 1; i < my_vals.length; i += 2){          int my_age = Integer.parseInt(my_vals[i]);          String name = my_vals[i - 1];          if (my_map.containsKey(my_age)){             ArrayList my_list = my_map.get(my_age);             my_list.add(name);           ... Read More

Lay Out Views in RelativeLayout Programmatically in Android

Azhar
Updated on 09-Jul-2020 06:41:14

887 Views

This example demonstrates how to lay out Views in RelativeLayout programmatically in Android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.javapackage com.app.sample; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; import android.widget.Toast; public class MainActivity extends Activity {    RelativeLayout relativelayout;    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);     ... Read More

Unreachable Code Error in Java

AmitDiwan
Updated on 09-Jul-2020 06:40:33

1K+ Views

Unreachable code error occurs when the code can’t be compiled due to a variety of reasons, some of which include: infinite loop, return statement before the unreachable line of code.Let us see an example −Example Live Demopublic class Demo{    public static void main(String args[]){       int val = 5;       for (;;){          if (val == 5){             break;             System.out.println("If the condition is not true, this line would be printed. ");          }       }    } ... Read More

Implement Custom Deserializer using @JsonDeserialize Annotation in Java

raja
Updated on 09-Jul-2020 06:19:40

3K+ Views

The @JsonDeserialize annotation is used to declare custom deserializer while deserializing JSON to Java object. We can implement a  custom deserializer by extending the StdDeserializer class with a generic type Employee and need to override the deserialize() method of StdDeserializer class.Syntax@Target(value={ANNOTATION_TYPE, METHOD, FIELD, TYPE, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonDeserializeIn the below program, we can implement a custom deserializer using @JsonDeserialize annotationExampleimport java.io.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.*; import com.fasterxml.jackson.databind.deser.std.*; public class JsonDeSerializeAnnotationTest {    public static void main (String[] args) throws JsonProcessingException, IOException {       Employee emp = new Employee(115, "Adithya");       ObjectMapper mapper = new ObjectMapper();     ... Read More

Advertisements