|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +A minimalistic yet highly functional JSON library |
| 5 | + |
| 6 | +# Features |
| 7 | + |
| 8 | +* Lightweight - small filesize and no dependencies and quick parsing |
| 9 | +* No boilerplate - as simplistic as possible, yet very flexible |
| 10 | +* Easy mapping - Easily convert JSON to objects, and back |
| 11 | + |
| 12 | +# Examples |
| 13 | + |
| 14 | +Presuming you have the following JSON stored as a string called "jsonString": |
| 15 | + |
| 16 | +```json |
| 17 | +{ |
| 18 | + "name":"Bob", |
| 19 | + "age":25, |
| 20 | + "siblings":[ |
| 21 | + "Jeff", |
| 22 | + "Sarah" |
| 23 | + ], |
| 24 | + "address":{ |
| 25 | + "road":"Roadway Drive", |
| 26 | + "house":123, |
| 27 | + "city":"Townington", |
| 28 | + "country":"Germany" |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | +### Basic usage |
| 33 | + |
| 34 | +You can easily access fields using the JSON and JSONArray objects: |
| 35 | + |
| 36 | +```java |
| 37 | +JSON person = new JSON(jsonString); |
| 38 | +System.out.println(person.string("name")); // prints "Bob" |
| 39 | +System.out.println(person.array("siblings").string(0)); // prints "Jeff" |
| 40 | +System.out.println(person.json("address").string("city")); // prints "Townington" |
| 41 | +``` |
| 42 | + |
| 43 | +### Object mapping |
| 44 | + |
| 45 | +If you have the following classes, you are able to map the JSON object to a Person object. Notice the annotations, these will be elaborated upon later. |
| 46 | + |
| 47 | +```java |
| 48 | +public @JSONContainer class Person { |
| 49 | + private String name; |
| 50 | + private int age; |
| 51 | + private String[] siblings; |
| 52 | + private Address address; |
| 53 | + |
| 54 | + public String getName() { return name; } |
| 55 | + public int getAge() { return age; } |
| 56 | + public String[] getSiblings() { return siblings; } |
| 57 | + public Address getAddress() { return address; } |
| 58 | +} |
| 59 | +``` |
| 60 | +```java |
| 61 | +public class Address { |
| 62 | + @JSONField(jsonKey = "road") private String road; |
| 63 | + @JSONField(jsonKey = "house") private int house; |
| 64 | + @JSONField(jsonKey = "city") private String city; |
| 65 | + @JSONField(jsonKey = "country") private String country; |
| 66 | + private String something; |
| 67 | + |
| 68 | + public String getRoad() { return road; } |
| 69 | + public int getHouse() { return house; } |
| 70 | + public String getCity() { return city; } |
| 71 | + public String getCountry() { return country; } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +After creating the JSON object, you can then specify to marshal it to a Person object. |
| 76 | + |
| 77 | +```java |
| 78 | +Person personObject = person.marshal(Person.class); |
| 79 | +System.out.println(personObject.getAge()); // prints "25" |
| 80 | +``` |
| 81 | + |
| 82 | +The annotation `@JSONContainer` specifies that all JSON keys (unless specifically ignored) will be mapped to a field with the name name. |
| 83 | +`@JSONField` specifies that only the provided field is mapped to the key specified in the annotation. |
| 84 | + |
| 85 | +# Usage |
| 86 | + |
| 87 | +Add Kotys as a dependency to your project. *Replace {version} with the version you would like, a list can be found [here](https://github.com/Arraying/Kotys/releases).* |
| 88 | + |
| 89 | + |
| 90 | +### Maven |
| 91 | + |
| 92 | +```xml |
| 93 | +<repositories> |
| 94 | + <repository> |
| 95 | + <id>jitpack.io</id> |
| 96 | + <url>https://jitpack.io</url> |
| 97 | + </repository> |
| 98 | +</repositories> |
| 99 | +<dependencies> |
| 100 | + <dependency> |
| 101 | + <groupId>com.github.Arraying</groupId> |
| 102 | + <artifactId>Kotys</artifactId> |
| 103 | + <version>{version}</version> |
| 104 | + </dependency> |
| 105 | +</dependencies> |
| 106 | +``` |
| 107 | + |
| 108 | +### Gradle |
| 109 | + |
| 110 | +```gradle |
| 111 | +repositories { |
| 112 | + maven { |
| 113 | + url 'https://jitpack.io' |
| 114 | + } |
| 115 | +} |
| 116 | +dependencies { |
| 117 | + compile 'com.github.Arraying:Kotys:{version}' |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +# Development & Contribution |
| 122 | + |
| 123 | +I will be providing bugfixes and changes. If you'd like to contribute, great! Currently, the main focus on the library should be opimization. Just make any changes and PR. |
| 124 | + |
0 commit comments