Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by raja
Page 15 of 38
How to implement custom FieldNamingStrategy using Gson in Java?
The FieldNamingStrategy is a mechanism for providing custom field naming in Gson. This allows the client code to translate field names into a particular convention that is not supported as a normal Java field declaration rules. The translateName() method will prefix every field name with the string “pre_”.In the below example, we can implement the Custom FieldNamingStrategy.Exampleimport java.lang.reflect.Field; import com.google.gson.*; public class GsonFieldNamingStrategyTest { public static void main(String[] args) { Employee emp = new Employee(); emp.setEmpId(115); emp.setFirstName("Adithya"); emp.setLastName("Jai"); CustomFieldNamingStrategy customFieldNamingStrategy = new CustomFieldNamingStrategy(); ...
Read MoreHow to get the JsonFactory settings using Jackson in Java?
The JsonFactory class is a thread-safe and responsible for creating instances of writer and reader. The list of settings that can be turned on/off is present in an enumeration JsonFactory.Feature, it contains static method values() that return the enum constant of this type with the specified name.Syntaxpublic static enum JsonFactory.Feature extends EnumExampleimport com.fasterxml.jackson.core.JsonFactory; public class JsonFactorySettingsTest { public static void main(String[] args) { JsonFactory jsonFactory = new JsonFactory(); for(JsonFactory.Feature feature : JsonFactory.Feature.values()) { boolean result = jsonFactory.isEnabled(feature); System.out.println(feature.name() + ":" + result); } } }OutputINTERN_FIELD_NAMES:true ...
Read MoreFieldNamingPolicy enum using Gson in Java?
Gson library provides the naming conventions as part of enum FieldNamingPolicy. We can set the field naming policy using the setFieldNamingPolicy() method of the GsonBuilder class.FieldNamingPolicy enum ConstantsIDENTITY − Using this naming policy, the field name is unchanged.LOWER_CASE_WITH_DASHES − Using this naming policy, modify the Java Field name from its camel-cased form to a lower case field name where each word is separated by a dash (-).LOWER_CASE_WITH_UNDERSCORES − Using this naming policy, modify the Java Field name from its camel-cased form to a lower case field name where each word is separated by an underscore (_).UPPER_CAMEL_CASE − Using this naming policy, ...
Read MoreHow to get the values of a key using the JsonPointer interface in Java?
The JSONPointer is a standard that defines a string syntax that can be used to access a particular key value in the JSON document. An instance of JSONPointer can be created by calling the static factory method createPointer() on the Json class. In the JSONPointer, every string syntax is prefixed with “/”. We can get the value of a key by calling the getValue() method on the JsonPointer object.JSON fileExampleimport javax.json.*; import java.io.*; public class JsonPointerTest { public static void main(String[] args) throws Exception { JsonReader jsonReader = Json.createReader(new FileReader("simple.json")); JsonStructure jsonStructure = jsonReader.read(); JsonPointer jsonPointer1 ...
Read MoreImportance of the JsonPatch interface in Java?
The JsonPatch interface is a format for storing a sequence of operations that can be applied to the target JSON structure. There are few operations like add, remove, replace, copy, move and test can be stored in JsonPath and operated on JSON structure. The JsonPatchBuilder interface can be used for constructing a JSON patch using the Json.createPatchBuilder().JSON fileExampleimport java.io.*; import javax.json.Json; import javax.json.JsonPatch; import javax.json.JsonPatchBuilder; import javax.json.JsonReader; import javax.json.JsonStructure; public class JsonPatchTest { public static void main(String[] args) throws Exception { JsonPatchBuilder jsonPatchBuilder = Json.createPatchBuilder(); JsonPatch jsonPatch = jsonPatchBuilder.add("/postalCode", "500072").remove("/age").build(); JsonReader reader = Json.createReader(new FileReader("simple.json")); ...
Read MoreHow to create a JSON using JsonObjectBuilder and JsonArrayBuilder in Java?
The JsonObjectBuilder can be used for creating JsonObject models whereas the JsonArrayBuilder can be used for creating JsonArray models. The JsonObjectBuilder can be created using the Json class, it contains methods to create the builder object and build an empty JsonObject instance using the Json.createObjectBuilder().build(). The JsonArrayBuilder can be created using the Json class, it contains methods to create the builder object and build an empty JsonArray instance using Json.createArrayBuilder().build().Exampleimport java.io.*; import javax.json.*; public class JsonObjectTest { public static void main(String[] args) { JsonObject empObject = Json.createObjectBuilder().add("empName", "Jai") .add("empAge", ...
Read MoreHow to parse a JSON string using Streaming API in Java?
The Streaming API consists of an important interface JsonParser and this interface contains methods to parse JSON in a streaming way and provides forward, read-only access to JSON data. The Json class contains the methods to create parsers from input sources. We can parse a JSON using the static method createParser() of Json class.Syntaxpublic static JsonParser createParser(Reader reader)Exampleimport java.io.*; import javax.json.Json; import javax.json.stream.JsonParser; import javax.json.stream.JsonParser.Event; public class JSONParseringTest { public static void main(String[] args) { String jsonString = "{\"name\":\"Adithya\", \"employeeId\":\"115\", \"age\":\"30\"}"; JsonParser parser = Json.createParser(new StringReader(jsonString)); while(parser.hasNext()) { Event event = ...
Read MoreHow to create a JSON Object using Object Model in Java?
The javax.json.JsonObject interface can represent an immutable JSON object value and provides an unmodifiable map view to the JSON object name/value mappings. A JsonObject instance can be created from an input source using the static method readObject() of javax.json.JsonReader class and also can be created using the static method createObjectBuilder() of javax.json.Json class.Syntaxpublic static JsonObjectBuilder createObjectBuilder()Exampleimport java.io.*; import javax.json.*; public class JsonObjectTest { public static void main(String[] args) throws Exception { JsonObjectBuilder builder = Json.createObjectBuilder(); builder.add("Name", "Adithya"); builder.add("Designation", "Python Developer"); builder.add("Company", "TutorialsPoint"); builder.add("Location", "Hyderabad"); JsonObject data = builder.build(); ...
Read MoreImportance of the accumulate() method of JSONObject in Java?
A JSONObject is an unordered collection of a name and value pairs. A few important methods of JSONArray are accumulate(), put(), opt(), append(), write() and etc. The accumulate() method accumulates the values under a key and this method similar to the put() method except if there is an existing object stored under a key then a JSONArray can be stored under a key to hold all of the accumulated values. If there is an existing JSONArray then a new value can be added.Syntaxpublic JSONObject accumulate(java.lang.String key, java.lang.Object value) throws JSONExceptionExampleimport org.json.*; public class JSONAccumulateMethodTest { public static void main(String[] args) throws JSONException { ...
Read MoreHow to convert a JSON object to an enum using Jackson in Java?
A JSONObject can parse the text from String to produce a Map kind of an object. An Enum can be used to define a collection of constants, when we need a predefined list of values which do not represent some kind of numeric or textual data then we can use an enum. We can convert a JSON object to an enum using the readValue() method of ObjectMapper class.In the below example, we can convert/deserialize a JSON object to Java enum using the Jackson library.Exampleimport com.fasterxml.jackson.databind.*; public class JSONToEnumTest { public static void main(String arg[]) throws Exception { ObjectMapper mapper ...
Read More