Code Monkey home page Code Monkey logo

java-json's Introduction

java-json

ulrichbarnstedt.lib.json

A small library to make converting classes to JSON easier.
Supports inheritance - see explanation below examples.
Note: Classes themselves can also be annotated for use cases such as deserializing the data later on.

Extending functionality

Classes which cannot be converted into JSON using the existing annotations, but should still be integrated into this ecosystem can be created using the JSONSerializable interface.
Example implementation:

package ulrichbarnstedt.test;

import ulrichbarnstedt.lib.json.JSONSerializable;

public class ClassWithCustomSerializer implements JSONSerializable {

    @Override
    public String toJSON (int indent) {
        // return the JSON of this class at the correct indent
    }
}

Example code

package ulrichbarnstedt.test;

import ulrichbarnstedt.lib.json.AnnotationJSONSerializer;
import ulrichbarnstedt.lib.json.JSONObject;
import ulrichbarnstedt.lib.json.JSONProperty;

//this annotation integrates this class into the ecosystem, meaning if it is a property of another JSON object
//it will automatically be converted to JSON, else toString of the class is used
//generally speaking, without this annotation the class will not even be attempted to be converted to JSON
@JSONObject
//writing this annotation at class scope will include the class name in the generated JSON
//supports a custom key like all other properties
@JSONProperty(key = "className")
public class Human {
    //a normal property to be included when converting to JSON
    @JSONProperty private String firstName;
    
    //a property to be included, but with a custom key
    @JSONProperty(key = "lAsTnAmE") private String lastName;
    
    //a second Human, as a property
    //will be converted to a JSON object since this class is annotated with @JSONObject
    @JSONProperty private Human someOtherGuy;
    
    @JSONProperty private long socSecNum;

    public Human (String firstName, String lastName, long socSecNum, Human someOtherGuy) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.socSecNum = socSecNum;
        this.someOtherGuy = someOtherGuy;
    }

    public static void main (String[] args) {
        Human testHuman = new Human("sub", "guy", 54321, null);
        Human human = new Human("jef", "jef", 12345, testHuman);
        
        //using the standard serializer to convert the class to JSON
        System.out.println(AnnotationJSONSerializer.toJSON(human));
        
        //alternative ways for converting, IF THE CLASS EXTENDS AnnotationJSONSerializer
        //it is not necessary as visible in the way above, but in some cases you might want to use this
        
        //use the method which was inherited, will also work for other classes which inherit from this
        //warning: if this class is not annotated with @JSONObject as explained above, this will result in toString() surrounded with "
        System.out.println(this.toJSON());
        
        //since static methods are inherited this works
        System.out.println(Human.toJSON(this));
    }
}

Outputs:

{
  "class" : "Human",
  "firstName" : "jef",
  "lAsTnAmE" : "jef",
  "someOtherGuy" : {
    "class" : "Human",
    "firstName" : "sub",
    "lAsTnAmE" : "guy",
    "someOtherGuy" : null,
    "socSecNum" : 54321
  },
  "socSecNum" : 12345
}

Inheritance is not explained in the example, but works as expected and will include all inherited properties until it reaches a class which is not annotated with @JSONObject.

java-json's People

Contributors

ulrich-barnstedt avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.