31 Mayıs 2023 Çarşamba

Object Model API For JSON

Giriş
Yeni paket ismi jakarta.json. Açıklaması şöyle
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.
Açıklaması şöyle
The Javax.json package is a collection of all the utilities that are available for use in Java environments to process JSON. It includes facilities such as getting immutable objects or event streams by parsing input streams, feeding output streams with these immutable objects or event streams, building immutable objects using builders, and navigating immutable objects.
Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.json</artifactId>
  <version>1.1.2</version>
</dependency>
JsonGenerator Arayüzü
Açıklaması şöyle
We can use the JsonGenerator interface to write the JSON data to an output in a streaming way. 
JsonReader Arayüzü
read metodu
Örnek
Şöyle yaparız
// Read json and create get the structure from it
JsonReader reader = Json.createReader(new FileReader("src/main/resources/profile.json"));
JsonStructure jsonStructure = reader.read();
JsonPointer Arayüzü
Bir anlamda XPATH gibidir.

add metodu
Örnek
Şöyle yaparız
JsonStructure jsonStructure = ...

// add a new value
JsonPointer agePointer = Json.createPointer("/age");
JsonNumber age = Json.createValue(30);
jsonStructure = agePointer.add(jsonStructure, age);
System.out.println(jsonStructure);

JsonPointer skillsPointer = Json.createPointer("/skills/-");
JsonString skill = Json.createValue("JsonPointer");
jsonStructure = skillsPointer.add(jsonStructure, skill);
System.out.println(jsonStructure);
containsValue metodu
Örnek
Şöyle yaparız
JsonStructure jsonStructure = ...

// checking a key exists
JsonPointer fakeKeyPointer = Json.createPointer("/fake");
boolean found = fakeKeyPointer.containsValue(jsonStructure);
System.out.println(found);

JsonPointer outOfBounds = Json.createPointer("/projects/3");
boolean foundOOB = outOfBounds.containsValue(jsonStructure);
System.out.println(foundOOB);
getValue metodu
Örnek
Şöyle yaparız
JsonStructure jsonStructure = ...

// getting a value from path

// using just object keys
JsonPointer cityPointer = Json.createPointer("/address/city"); 
JsonString city = (JsonString) cityPointer.getValue(jsonStructure);
System.out.println(city.getString());

// using just array indexes
JsonPointer projectTwoPointer = Json.createPointer("/projects/1"); 
JsonObject project = (JsonObject) projectTwoPointer.getValue(jsonStructure);
System.out.println(project.toString());
remove metodu
Örnek
Şöyle yaparız
JsonStructure jsonStructure = ...

// remove a field
JsonPointer addressPointer = Json.createPointer("/address");
jsonStructure = addressPointer.remove(jsonStructure);
System.out.println(jsonStructure);
replace metodu
Örnek
Şöyle yaparız
JsonStructure jsonStructure = ...

// change a value
JsonPointer namePointer = Json.createPointer("/name");
JsonString name = Json.createValue("Bill Nye");
jsonStructure = namePointer.replace(jsonStructure, name);
System.out.println(jsonStructure);

Hiç yorum yok:

Yorum Gönder

Bean Validation @GroupSequence Anotasyonu

Örnek Elimizde şöyle bir kod olsun public class SampleRequest {   @NotNull   LocalDate startDate;   @NotNull   LocalDate endDate;   @AssertT...