Skip to content

Java library to serialize and deserialize Java objects to (and from) JSON

License

Notifications You must be signed in to change notification settings

fedorchuck/dnjson

Repository files navigation

Java library to serialize and deserialize Java objects to (and from) JSON

Build Status Apache License Version 2.0 Maven Central Codacy Badge Codacy Badge

Introduction

DNJson uses reflection so it does not require additional modifications to classes of (de)serialized objects. In fact it just needs the class to have defined default no-args constructor (not entirely true, see Features).

This library compatible with Java 6+

Contents

Getting started

Download

Gradle:

compile 'com.github.fedorchuck:dnjson:0.1.0'

Maven:

<dependency>
  <groupId>com.github.fedorchuck</groupId>
  <artifactId>dnjson</artifactId>
  <version>0.1.0</version>
</dependency>

JAR-files:
https://oss.sonatype.org/content/repositories/releases/com/github/fedorchuck/dnjson/

Usage

The following example demonstrates the most basic usage of DNJson when serializing a sample object:

    public class Car {
        private String manufacturer;
        private String model;
        private Double capacity;
        private boolean accident;
    
        private Car() {
        }
    
        public Car(String manufacturer, String model, Double capacity, boolean accident) {
            this.manufacturer = manufacturer;
            this.model = model;
            this.capacity = capacity;
            this.accident = accident;
        }
    
        @Override
        public String toString() {
            return("Manufacturer: " + manufacturer + ", " + "Model: " + model + ",
                    " + "Capacity: " + capacity + ", " + "Accident: " + accident);
        }
    }
    
    public class Person {
        private String name;
        private String surname;
        private Car[] cars;
        private int phone;
        private transient int age;
    
        private Person() {
        }
    
        public Person(String name, String surname, int phone, int age, Car[] cars) {
            this.name = name;
            this.surname = surname;
            this.cars = cars;
            this.phone = phone;
            this.age = age;
        }
    
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
    
            sb.append("Name: " + name + " " + surname + "\n");
            sb.append("Phone: " + phone + "\n");
            sb.append("Age: " + age + "\n");
    
            int i = 0;
            for (Car item : cars) {
                i++;
                sb.append("Car " + i + ": " + item + "\n");
            }
    
            return sb.toString();
        }
    }

After calling

    DNJson dnjson = new DNJson();
    Car audi = new Car("Audi", "A4", 1.8, false);
    Car skoda = new Car("Škoda", "Octavia", 2.0, true);
    Car[] cars = {audi, skoda};
    Person johnDoe = new Person("John", "Doe", 245987453, 35, cars);
    System.out.println(dnjson.toJson(johnDoe));

you will get this output:

    {
        "name": "John",
        "surname": "Doe",
        "cars": [
            {
                "manufacturer": "Audi",
                "model": "A4",
                "capacity": 1.8,
                "accident": false
            },
            {
                "manufacturer": "Škoda",
                "model": "Octavia",
                "capacity": 2,
                "accident": true
            }
        ],
        "phone": 245987453
    }

Since the Person's field age is marked as transient, it is not included in the output.

To deserialize output produced by last example, you can execute the following code:

    DNJson dnjson = new DNJson();
    String json =
    "{\"name\":\"John\",\"surname\":\"Doe\",\"cars\":
    [{\"manufacturer\":\"Audi\",\"model\":\"A4\",\"capacity\":1.8,\"accident\":false},
    {\"manufacturer\":\"Škoda\",\"model\":\"Octavia\",\"capacity\":2.0,\"accident\":true}],
    \"phone\":245987453}";
    Person johnDoe = dnjson.fromJson(json, Person.class);
    System.out.println(johnDoe.toString());

And the following output will be generated:

    Name: John Doe
    Phone: 245987453
    Age: 0
    Car 1: Manufacturer: Audi, Model: A4, Capacity: 1.8, Accident: false
    Car 2: Manufacturer: Škoda, Model: Octavia, Capacity: 2.0, Accident: true

Changelog

See changelog file

License

This software is licensed under Apache License 2.0

About

Java library to serialize and deserialize Java objects to (and from) JSON

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages