dnjson

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

Лицензия

Лицензия

Категории

Категории

JSON Данные
Группа

Группа

com.github.fedorchuck
Идентификатор

Идентификатор

dnjson
Последняя версия

Последняя версия

0.1.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

dnjson
Java library to serialize and deserialize Java objects to (and from) JSON
Ссылка на сайт

Ссылка на сайт

https://github.com/fedorchuck/dnjson
Система контроля версий

Система контроля версий

https://github.com/fedorchuck/dnjson

Скачать dnjson

Как подключить последнюю версию

<!-- 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"]

Зависимости

Библиотека не имеет зависимостей. Это самодостаточное приложение, которое не зависит ни от каких других библиотек.

Модули Проекта

Данный проект не имеет модулей.

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

Версии библиотеки

Версия
0.1.0