forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#14405 from tudormarc/tudor/jacksonjr4
BAEL-6310 - updated indent
- Loading branch information
Showing
10 changed files
with
126 additions
and
119 deletions.
There are no files selected for viewing
96 changes: 0 additions & 96 deletions
96
jackson-jr/src/main/java/com/baeldung/jacksonjr/JacksonJrFeatures.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
...ung/jacksonjr/CustomDateDeserializer.java → ...ung/jacksonjr/CustomDateDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
package com.baeldung.jacksonjr; | ||
|
||
import com.fasterxml.jackson.jr.ob.api.ValueReader; | ||
import com.fasterxml.jackson.jr.ob.impl.JSONReader; | ||
import com.fasterxml.jackson.jr.private_.JsonParser; | ||
|
||
import java.io.IOException; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import com.fasterxml.jackson.jr.ob.api.ValueReader; | ||
import com.fasterxml.jackson.jr.ob.impl.JSONReader; | ||
import com.fasterxml.jackson.jr.private_.JsonParser; | ||
|
||
public class CustomDateDeserializer extends ValueReader { | ||
private final static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | ||
|
||
public CustomDateDeserializer () { | ||
public CustomDateDeserializer() { | ||
super(LocalDate.class); | ||
} | ||
|
||
@Override | ||
public Object read (JSONReader jsonReader, JsonParser jsonParser) throws IOException { | ||
public Object read(JSONReader jsonReader, JsonParser jsonParser) throws IOException { | ||
return LocalDate.parse(jsonParser.getText(), dtf); | ||
} | ||
} |
10 changes: 5 additions & 5 deletions
10
...ldung/jacksonjr/CustomDateSerializer.java → ...ldung/jacksonjr/CustomDateSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
package com.baeldung.jacksonjr; | ||
|
||
import java.io.IOException; | ||
import java.time.LocalDate; | ||
|
||
import com.fasterxml.jackson.jr.ob.api.ValueWriter; | ||
import com.fasterxml.jackson.jr.ob.impl.JSONWriter; | ||
import com.fasterxml.jackson.jr.private_.JsonGenerator; | ||
|
||
import java.io.IOException; | ||
import java.time.LocalDate; | ||
|
||
public class CustomDateSerializer implements ValueWriter { | ||
@Override | ||
public void writeValue (JSONWriter jsonWriter, JsonGenerator jsonGenerator, Object o) throws IOException { | ||
public void writeValue(JSONWriter jsonWriter, JsonGenerator jsonGenerator, Object o) throws IOException { | ||
jsonGenerator.writeString(o.toString()); | ||
} | ||
|
||
@Override | ||
public Class<?> valueType () { | ||
public Class<?> valueType() { | ||
return LocalDate.class; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
jackson-modules/jackson-jr/src/main/java/com/baeldung/jacksonjr/JacksonJrFeatures.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.baeldung.jacksonjr; | ||
|
||
import java.io.IOException; | ||
import java.util.LinkedHashMap; | ||
|
||
import com.fasterxml.jackson.jr.annotationsupport.JacksonAnnotationExtension; | ||
import com.fasterxml.jackson.jr.ob.JSON; | ||
import com.fasterxml.jackson.jr.ob.JacksonJrExtension; | ||
import com.fasterxml.jackson.jr.ob.api.ExtensionContext; | ||
|
||
public class JacksonJrFeatures { | ||
|
||
public static String jsonObject() throws IOException { | ||
return JSON.std.with(JSON.Feature.PRETTY_PRINT_OUTPUT) | ||
.asString(new LinkedHashMap<String, Object>() {{ | ||
put("name", "John Doe"); | ||
put("age", 30); | ||
}}); | ||
} | ||
|
||
public static String jsonComposer() throws IOException { | ||
return JSON.std.with(JSON.Feature.PRETTY_PRINT_OUTPUT) | ||
.composeString() | ||
.startObject() | ||
.startArrayField("objectArray") | ||
.startObject() | ||
.put("name", "name1") | ||
.put("age", 11) | ||
.end() | ||
.startObject() | ||
.put("name", "name2") | ||
.put("age", 12) | ||
.end() | ||
.end() | ||
.startArrayField("array") | ||
.add(1) | ||
.add(2) | ||
.add(3) | ||
.end() | ||
.startObjectField("object") | ||
.put("name", "name3") | ||
.put("age", 13) | ||
.end() | ||
.put("last", true) | ||
.end() | ||
.finish(); | ||
} | ||
|
||
public static String objectSerialization(Person person) throws IOException { | ||
return JSON.std.with(JSON.Feature.PRETTY_PRINT_OUTPUT) | ||
.asString(person); | ||
} | ||
|
||
public static String objectAnnotationSerialization(Person person) throws IOException { | ||
return JSON.builder() | ||
.register(JacksonAnnotationExtension.std) | ||
.build() | ||
.with(JSON.Feature.PRETTY_PRINT_OUTPUT) | ||
.asString(person); | ||
} | ||
|
||
public static String customObjectSerialization(Person person) throws IOException { | ||
return JSON.builder() | ||
.register(new JacksonJrExtension() { | ||
@Override | ||
protected void register(ExtensionContext extensionContext) { | ||
extensionContext.insertProvider(new MyHandlerProvider()); | ||
} | ||
}) | ||
.build() | ||
.with(JSON.Feature.PRETTY_PRINT_OUTPUT) | ||
.asString(person); | ||
} | ||
|
||
public static Person objectDeserialization(String json) throws IOException { | ||
return JSON.std.with(JSON.Feature.PRETTY_PRINT_OUTPUT) | ||
.beanFrom(Person.class, json); | ||
} | ||
|
||
public static Person customObjectDeserialization(String json) throws IOException { | ||
return JSON.builder() | ||
.register(new JacksonJrExtension() { | ||
@Override | ||
protected void register(ExtensionContext extensionContext) { | ||
extensionContext.insertProvider(new MyHandlerProvider()); | ||
} | ||
}) | ||
.build() | ||
.with(JSON.Feature.PRETTY_PRINT_OUTPUT) | ||
.beanFrom(Person.class, json); | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
...baeldung/jacksonjr/MyHandlerProvider.java → ...baeldung/jacksonjr/MyHandlerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...n/java/com/baeldung/jacksonjr/Person.java → ...n/java/com/baeldung/jacksonjr/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
.../jacksonjr/JacksonJrFeaturesUnitTest.java → .../jacksonjr/JacksonJrFeaturesUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters