Skip to content

pwalser75/json-xml-yaml-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON/XML/YAML Test

Testing Jackson JSON/XML/YAML object mapping

Features

  • shows how to properly configure the Jackson ObjectMapper (with Java 8 Date/Time API support)
  • demonstrates how to serialize/deserialize JSON, XML and YAML with Jackson
  • bean serialization → Movie, Actor
  • custom serialization for enums using arbitrary string identifiers, ignoring case → Genre
  • custom serialization for domain objectMetadata with MetadataConverter.Serializer and MetadataConverter.Deserializer
  • custom serialization for immutable value objects with string constructor / toString()AspectRatio
  • custom serialization for java.time.Duration in a user-friendly format (e.g. "1w 2d 3h 4m 5s")
  • function-based custom serialization using FunctionalSerializer / FunctionalDeserializer
  • NEW: JSON schema and validation example

Examples

Serialized examples (Movie with Actor, Genre, AspectRatio and Metadata):

JSON

887 bytes, 3.14 µS serialization, 5.39 µS deserialization

{
  "title": "Blade Runner",
  "year": 1982,
  "genres": [
    "Sci-Fi",
    "Thriller"
  ],
  "aspect-ratio": "2.39:1",
  "created": "2021-05-18T12:27:16Z",
  "ratings": {
    "IMDB": 8.2,
    "Metacritic": 89.0
  },
  "synopsis": "A blade runner must pursue and terminate four replicants\n who stole a ship in space and have returned to Earth to find their creator.",
  "actors": [
    {
      "firstName": "Harrison",
      "lastName": "Ford",
      "dateOfBirth": "1942-07-13",
      "age": 78
    },
    {
      "firstName": "Rutger",
      "lastName": "Hauer",
      "dateOfBirth": "1944-01-23",
      "age": 77
    },
    {
      "firstName": "Sean",
      "lastName": "Young",
      "dateOfBirth": "1959-11-20",
      "age": 61
    }
  ],
  "duration": "1h 57m",
  "metadata": {
    "director": "Ridley Scott",
    "release-date": "1982-06-25",
    "screenplay": "Hampton Fancher, David Webb Peoples"
  }
}

XML

1008 bytes, 4.88 µS serialization, 12.34 µS deserialization

<movie>
    <ratings IMDB="8.2" Metacritic="89.0"/>
    <created>2021-05-18T12:27:12Z</created>
    <title>Blade Runner</title>
    <year>1982</year>
    <genre>Sci-Fi</genre>
    <genre>Thriller</genre>
    <aspectRatio>2.39:1</aspectRatio>
    <synopsis>A blade runner must pursue and terminate four replicants
        who stole a ship in space and have returned to Earth to find their creator.
    </synopsis>
    <actor age="78">
        <firstName>Harrison</firstName>
        <lastName>Ford</lastName>
        <dateOfBirth>1942-07-13</dateOfBirth>
    </actor>
    <actor age="77">
        <firstName>Rutger</firstName>
        <lastName>Hauer</lastName>
        <dateOfBirth>1944-01-23</dateOfBirth>
    </actor>
    <actor age="61">
        <firstName>Sean</firstName>
        <lastName>Young</lastName>
        <dateOfBirth>1959-11-20</dateOfBirth>
    </actor>
    <duration>1h 57m</duration>
    <metadata>
        <director>Ridley Scott</director>
        <release-date>1982-06-25</release-date>
        <screenplay>Hampton Fancher, David Webb Peoples</screenplay>
    </metadata>
</movie>

YAML

665 bytes, 17.66 µS serialization, 39.16 µS deserialization

---
title: Blade Runner
year: 1982
genres:
  - Sci-Fi
  - Thriller
aspect-ratio: 2.39:1
created: 2021-05-18T12:27:18Z
ratings:
  IMDB: 8.2
  Metacritic: 89.0
synopsis: |-
  A blade runner must pursue and terminate four replicants
   who stole a ship in space and have returned to Earth to find their creator.
actors:
  - firstName: Harrison
    lastName: Ford
    dateOfBirth: 1942-07-13
    age: 78
  - firstName: Rutger
    lastName: Hauer
    dateOfBirth: 1944-01-23
    age: 77
  - firstName: Sean
    lastName: Young
    dateOfBirth: 1959-11-20
    age: 61
duration: 1h 57m
metadata:
  director: Ridley Scott
  release-date: 1982-06-25
  screenplay: "Hampton Fancher, David Webb Peoples"

Java Properties

726 bytes, 5.27 µS serialization, 16.06 µS deserialization

title=Blade Runner
year=1982
genres.1=Sci-Fi
genres.2=Thriller
aspect-ratio=2.39:1
created=2021-05-18T12:27:10Z
ratings.IMDB=8.2
ratings.Metacritic=89.0
synopsis=A blade runner must pursue and terminate four replicants\n who stole a ship in space and have returned to Earth to find their creator.
actors.1.firstName=Harrison
actors.1.lastName=Ford
actors.1.dateOfBirth=1942-07-13
actors.1.age=78
actors.2.firstName=Rutger
actors.2.lastName=Hauer
actors.2.dateOfBirth=1944-01-23
actors.2.age=77
actors.3.firstName=Sean
actors.3.lastName=Young
actors.3.dateOfBirth=1959-11-20
actors.3.age=61
duration=1h 57m
metadata.director=Ridley Scott
metadata.release-date=1982-06-25
metadata.screenplay=Hampton Fancher, David Webb Peoples

607 bytes, 2.39 µS serialization, 4.91 µS deserialization

BF 65 74 69 74 6C 65 6C 42 6C 61 64 65 20 52 75 6E 6E 65 72 64 79 65 61 72 19 07 BE 66 67 65 6E 
72 65 73 82 66 53 63 69 2D 46 69 68 54 68 72 69 6C 6C 65 72 6C 61 73 70 65 63 74 2D 72 61 74 69 
6F 66 32 2E 33 39 3A 31 67 63 72 65 61 74 65 64 74 32 30 32 31 2D 30 35 2D 31 38 54 31 32 3A 32 
37 3A 31 34 5A 67 72 61 74 69 6E 67 73 BF 64 49 4D 44 42 FB 40 20 66 66 66 66 66 66 6A 4D 65 74 
61 63 72 69 74 69 63 FB 40 56 40 00 00 00 00 00 FF 68 73 79 6E 6F 70 73 69 73 78 85 41 20 62 6C 
61 64 65 20 72 75 6E 6E 65 72 20 6D 75 73 74 20 70 75 72 73 75 65 20 61 6E 64 20 74 65 72 6D 69 
6E 61 74 65 20 66 6F 75 72 20 72 65 70 6C 69 63 61 6E 74 73 0A 20 77 68 6F 20 73 74 6F 6C 65 20 
61 20 73 68 69 70 20 69 6E 20 73 70 61 63 65 20 61 6E 64 20 68 61 76 65 20 72 65 74 75 72 6E 65 
64 20 74 6F 20 45 61 72 74 68 20 74 6F 20 66 69 6E 64 20 74 68 65 69 72 20 63 72 65 61 74 6F 72 
2E 66 61 63 74 6F 72 73 83 BF 69 66 69 72 73 74 4E 61 6D 65 68 48 61 72 72 69 73 6F 6E 68 6C 61 
73 74 4E 61 6D 65 64 46 6F 72 64 6B 64 61 74 65 4F 66 42 69 72 74 68 6A 31 39 34 32 2D 30 37 2D 
31 33 63 61 67 65 18 4E FF BF 69 66 69 72 73 74 4E 61 6D 65 66 52 75 74 67 65 72 68 6C 61 73 74 
4E 61 6D 65 65 48 61 75 65 72 6B 64 61 74 65 4F 66 42 69 72 74 68 6A 31 39 34 34 2D 30 31 2D 32 
33 63 61 67 65 18 4D FF BF 69 66 69 72 73 74 4E 61 6D 65 64 53 65 61 6E 68 6C 61 73 74 4E 61 6D 
65 65 59 6F 75 6E 67 6B 64 61 74 65 4F 66 42 69 72 74 68 6A 31 39 35 39 2D 31 31 2D 32 30 63 61 
67 65 18 3D FF 68 64 75 72 61 74 69 6F 6E 66 31 68 20 35 37 6D 68 6D 65 74 61 64 61 74 61 BF 68 
64 69 72 65 63 74 6F 72 6C 52 69 64 6C 65 79 20 53 63 6F 74 74 6C 72 65 6C 65 61 73 65 2D 64 61 
74 65 6A 31 39 38 32 2D 30 36 2D 32 35 6A 73 63 72 65 65 6E 70 6C 61 79 78 23 48 61 6D 70 74 6F 
6E 20 46 61 6E 63 68 65 72 2C 20 44 61 76 69 64 20 57 65 62 62 20 50 65 6F 70 6C 65 73 FF FF 

Build

Gradle:

Build with gradle (default: clean build):

./gradlew

Maven:

Build with Maven (default: clean install):

mvn

About

Testing Jackson XML/JSON/YAML object mapping

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages