dnjson

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

License

License

Categories

Categories

JSON Data
GroupId

GroupId

com.github.fedorchuck
ArtifactId

ArtifactId

dnjson
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

dnjson
Java library to serialize and deserialize Java objects to (and from) JSON
Project URL

Project URL

https://github.com/fedorchuck/dnjson
Source Code Management

Source Code Management

https://github.com/fedorchuck/dnjson

Download dnjson

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.fedorchuck/dnjson/ -->
<dependency>
    <groupId>com.github.fedorchuck</groupId>
    <artifactId>dnjson</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.fedorchuck/dnjson/
implementation 'com.github.fedorchuck:dnjson:0.1.0'
// https://jarcasting.com/artifacts/com.github.fedorchuck/dnjson/
implementation ("com.github.fedorchuck:dnjson:0.1.0")
'com.github.fedorchuck:dnjson:jar:0.1.0'
<dependency org="com.github.fedorchuck" name="dnjson" rev="0.1.0">
  <artifact name="dnjson" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.fedorchuck', module='dnjson', version='0.1.0')
)
libraryDependencies += "com.github.fedorchuck" % "dnjson" % "0.1.0"
[com.github.fedorchuck/dnjson "0.1.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

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

Versions

Version
0.1.0