Have you ever think about JSON with only 4 types: number, string, array, object? :)
What if your programming language or some another ecosystem doesn't support null or true/false as a types or a special values?
If it is your case then NSNJSON comes to help you! :)
The NSNJSON operates only 4 types - number, string, array, object!
Also, it defines types mapping table:
| JSON type | null | number | string | true | false | array | object |
|---|---|---|---|---|---|---|---|
| NSNJSON type | 0 | 1 | 2 | 3 | 3 | 4 | 5 |
For every JSON type NSNJSON defines special presentation which is absolutely valid JSON.
NSNJSON presentation is always JSON object.
Such object always contains field "t" (which means type of source JSON type).
Also, for all JSON types except null the object always contains field "v" (which means value of source JSON type).
JSON object field has a name. So, NSNJSON saves this information in field "n" (which means name of field JSON object).
More info on NSNJSON Driver page.
Use jitpack.io!
If you are using Maven, then add JitPack repository to your pom.xml:
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>Now, you can specify dependency:
<dependency>
<groupId>com.github.nsnjson</groupId>
<artifactId>nsnjson-java-driver</artifactId>
<version>v0.0.2</version>
</dependency>The driver uses Optional as a wrapper for encoding / decoding results.
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.*;
import com.github.nsnjson.decoding.Decoder;
import com.github.nsnjson.encoding.Encoder;
import java.util.Optional;
public class Main {
public static void main(String[] args) {
JsonNode data = NullNode.getInstance();
Optional<JsonNode> presentationOption = Encoder.encode(data);
System.out.println(presentationOption);
// Optional[{ "t": 0 }]
System.out.println(Decoder.decode(presentationOption.get()));
// Optional[null]
}
}You can define your own rules for JSON encoding/decoding.
Just pass custom rules as an argument to related methods:
- Driver.encode(JsonNode, Encoding)
- Driver.decode(JsonNode, Decoding)
- Encoder.encode(JsonNode, Encoding)
- Decoder.decode(JsonNode, Decoding)
Example:
package com.github.nsnjson;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.*;
import com.github.nsnjson.decoding.*;
import com.github.nsnjson.encoding.*;
import java.util.Optional;
public class Main {
public static void main(String[] args) {
JsonNode data = IntNode.valueOf(2015);
Encoding encoding = new DefaultEncoding() {
@Override
public Optional<JsonNode> encodeNumber(NumericNode value) {
ArrayNode presentation = new ObjectMapper().createArrayNode();
presentation.add("number");
presentation.add(value);
return Optional.of(presentation);
}
};
Decoding decoding = new DefaultDecoding() {
@Override
public Optional<JsonNodeType> getType(JsonNode presentation) {
if (presentation.isArray()) {
return Optional.of(JsonNodeType.NUMBER);
}
return super.getType(presentation);
}
@Override
public Optional<JsonNode> decodeNumber(JsonNode presentation) {
return Optional.of(presentation.get(0));
}
};
Optional<JsonNode> presentationOption = Encoder.encode(data, encoding);
System.out.println(presentationOption);
// Optional[["number",2015]]
System.out.println(Decoder.decode(presentationOption.get(), decoding));
// Optional["number"]
}
}