https://github.com/j4ts/j4ts-json
An implementation of the Gson json handler package
https://github.com/j4ts/j4ts-json
Last synced: 4 months ago
JSON representation
An implementation of the Gson json handler package
- Host: GitHub
- URL: https://github.com/j4ts/j4ts-json
- Owner: j4ts
- License: gpl-3.0
- Created: 2019-03-17T13:34:27.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-03-17T14:49:47.000Z (over 6 years ago)
- Last Synced: 2025-01-14T03:39:15.987Z (5 months ago)
- Language: Java
- Size: 21.5 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# j4ts-json
A JSweet implementation for https://github.com/google/gson json handlerexample:
```
package test;import com.google.gson.*;
import java.lang.reflect.Type;
import java.util.*;public class Test2 {
private final int num;
private final String msg;public Test2(int num, String msg) {
this.num = num;
this.msg = msg;
}public static class TestDeserializer implements JsonDeserializer {
@Override
public Test2 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonArray()) {
JsonArray asJsonArray = json.getAsJsonArray();
if (asJsonArray.size() >= 2)
return new Test2(context.deserialize(asJsonArray.get(0), Integer.class), context.deserialize(asJsonArray.get(1), String.class));
}
throw new JsonParseException("not a valid Test");
}
}public static class TestSerializer implements JsonSerializer {
@Override
public JsonElement serialize(Test2 src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(src.msg, src.num);
return jsonObject;
}// currently need this hack :'(
public JsonElement serialize(Test2 src, Object typeOfSrc, JsonSerializationContext context) {
return serialize(src, (Type) typeOfSrc, context);
}
}
public static void main(String[] args) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Test2.class, new TestDeserializer())
.registerTypeAdapter(Test2.class, new TestSerializer())
.create();Test2 test = gson.fromJson("[5, \"test\"]", Test2.class);
String s = gson.toJson(test);
System.err.println(s);if (!Objects.equals("{\"test\":5}", s))
throw new JsonParseException("Not equal :(");Map, ?> listSerializable = gson.fromJson("{\"a\": 1.1, \"b\": \"x\", \"c\": []}", Map.class);
List> values = new LinkedList<>(listSerializable.values());
String s1 = gson.toJson(values);
System.err.println(s1);if (!Objects.equals("[1.1,\"x\",[]]", s1))
throw new JsonParseException("Not equal :(");
}
}```
add maven dependency to your repo, and you can use it java such as javascript side
```com.google.code.gson
gson
2.8.5```