Giter VIP home page Giter VIP logo

jhocon's Introduction

Maven Central

JHocon

This is a Gson wrapper using Typesafe Config for supporting HOCON files. JHocon overrides Gson' JsonWriter and JsonReader and a gives full compatibility with Gson. JHocon providing simple helper functions to convert between Hocon and Java Objects. Also, supported all Gson' TypeAdapters, TypeAdapterFactories and annotations.

Usage

For example, we have some simple class.

public final class Person {
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

Converting non-generic object

JHocon jhocon = new JHoconBuilder().create();
Person person = new Person("foo", 20);

// Convert non-generic object to HOCON-string representation
String hocon = jhocon.toHocon("person", person);

// Create non-generic object from HOCON-string representation
Person personNew = jhocon.fromHocon(hocon, "person", Person.class);

Output hocon string:

person {
    age=20
    name=foo
}

Converting generic object

JHocon jhocon = new JHoconBuilder().create();
List<Person> family = new ArrayList<>();
family.add(new Person("foo", 20));
family.add(new Person("bar", 25));

// Convert generic object to HOCON-string representation.
String hocon = jhocon.toHocon("family", family);

// Create generic object from HOCON-string representation
Type type = new TypeToken<List<Person>>() {}.getType();
List<Person> familyNew = jhocon.fromHocon(hocon, "family", type);

Output hocon string:

family=[
    {
        age=20
        name=foo
    },
    {
        age=25
        name=bar
    }
]

Comments and field validators

Add special annotations above the class fields. And enable comments and default validators in JHoconBuilder. Also, you can register custom field handlers and field annotations. You can see more in tests.

@Comment("person name")
@ValidatorStringList(value = {"reserved"}, invert = true)
public String name;
@Comment
@ValidatorRange(min = 0, max = 150)
public int age;

// ...
new JHoconBuilder().withComments().registerDefaultValidators().create();
person {
    # default value: 20
    # valid range: [0, 150]
    age=20
    # person name
    # valid values: not [reserved]
    name=foo
}

jhocon's People

Contributors

dahaka934 avatar

Watchers

 avatar  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.