Found 2620 Articles for Java

How to get the JsonFactory settings using Jackson in Java?

raja
Updated on 07-Jul-2020 12:12:33

619 Views

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 More

FieldNamingPolicy enum using Gson in Java?

raja
Updated on 07-Jul-2020 12:04:50

723 Views

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 More

How to get the JsonGenerator settings using Jackson in Java?

raja
Updated on 07-Jul-2020 12:05:39

260 Views

The JsonGenerator class can be responsible for writing JSON data as a stream instead of constructing an Object Model in memory. The list of settings that can be turned on/off is present in an enum JsonGenerator.Feature, it contains static method values() that returns an array containing the constants of this enum type.Syntaxpublic static enum JsonGenerator.Feature extends EnumExampleimport java.io.*; import com.fasterxml.jackson.core.*; public class JsonGeneratorSettingsTest {    public static void main(String[] args) throws IOException {       StringWriter writer = new StringWriter();       JsonFactory jsonFactory = new JsonFactory();       JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer);       for(JsonGenerator.Feature feature : JsonGenerator.Feature.values()) {     ... Read More

How to get the values of a key using the JsonPointer interface in Java?

raja
Updated on 07-Jul-2020 11:58:26

527 Views

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 More

Importance of the JsonPatch interface in Java?

raja
Updated on 07-Jul-2020 11:55:50

449 Views

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 More

How to create a JSON using JsonObjectBuilder and JsonArrayBuilder in Java?

raja
Updated on 07-Jul-2020 11:43:40

7K+ Views

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 More

How can we implement a JSON array using Streaming API in Java?

raja
Updated on 18-Feb-2020 10:47:33

679 Views

The JsonGenerator interface can be used to write the JSON data to an output source in a streaming way. We can create or implement a JSON array using the writeStartArray() method of JsonGenerator, this method writes the JSON name/start array character pair within the current object context. The writeStartObject() method writes the JSON start object character, and valid only in an array context and the writeEnd() method writes the end of the current context.SyntaxJsonGenerator writeStartArray(String name)Exampleimport java.io.*; import javax.json.*; import javax.json.stream.*; public class JsonGeneratorTest {    public static void main(String[] args) throws Exception {       StringWriter writer = new ... Read More

Pretty print JSON using javax.json API in Java?

raja
Updated on 07-Jul-2020 07:58:47

645 Views

The javax.json package provides an Object Model API to process JSON. The Object Model API is a high-level API that provides immutable object models for JSON object and array structures. These JSON structures can be represented as object models using JsonObject and JsonArray interfaces. We can use the JsonGenerator interface to write the JSON data to an output in a streaming way. The JsonGenerator.PRETTY_PRINTING is a configuration property to generate JSON prettily.We can implement a pretty print JSON in the below example.Exampleimport java.io.*; import java.util.*; import javax.json.*; import javax.json.stream.*; public class JSONPrettyPrintTest {    public static void main(String args[]) {       String jsonString = ... Read More

How to parse a JSON string using Streaming API in Java?

raja
Updated on 07-Jul-2020 07:40:21

2K+ Views

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 More

How to create a JSON Object using Object Model in Java?

raja
Updated on 07-Jul-2020 07:25:17

2K+ Views

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 More

Advertisements