use of com.google.gson.JsonNull in project st-js by st-js.
the class JSArrayAdapter method serialize.
/**
* {@inheritDoc}
*/
@Override
public JsonElement serialize(Array<?> array, Type type, JsonSerializationContext ctx) {
if (array == null) {
return new JsonNull();
}
JsonArray js = new JsonArray();
// validate that we have a packed array (no unset elements) and that we do not
// have any non-array indices. JSON supports none of these features, and toList()
// detects them and rejects them too.
List<?> list = array.toList();
return ctx.serialize(list);
}
use of com.google.gson.JsonNull in project RxReddit by damien5314.
the class ModReportDeserializer method deserialize.
@Override
public ModReport deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonArray array = json.getAsJsonArray();
// Extract text element
JsonElement textElement = array.get(0);
String text = textElement instanceof JsonNull ? null : textElement.getAsString();
// Extract username element
String username = array.get(1).getAsString();
return new ModReport(text, username);
}
use of com.google.gson.JsonNull in project RxReddit by damien5314.
the class UserReportDeserializer method deserialize.
@Override
public UserReport deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonArray array = json.getAsJsonArray();
// Extract text element
JsonElement textElement = array.get(0);
String text = textElement instanceof JsonNull ? null : textElement.getAsString();
// Extract quantity element
int quantity = array.get(1).getAsInt();
return new UserReport(text, quantity);
}
use of com.google.gson.JsonNull in project be5 by DevelopmentOnTheEdge.
the class ParseRequestUtils method getValuesFromJson.
public static Map<String, Object> getValuesFromJson(String valuesString) throws Be5Exception {
if (Strings.isNullOrEmpty(valuesString)) {
return Collections.emptyMap();
}
Map<String, Object> fieldValues = new HashMap<>();
// todo gson -> json-b
// InputStream stream = new ByteArrayInputStream(valuesString.getBytes(StandardCharsets.UTF_8.name()));
// javax.json.stream.JsonParser parser = Json.createParser(stream);
//
// javax.json.JsonObject object = parser.getObject();
// Set<Map.Entry<String, JsonValue>> entries = object.entrySet();
JsonObject values = (JsonObject) new JsonParser().parse(valuesString);
for (Map.Entry entry : values.entrySet()) {
String name = entry.getKey().toString();
if (entry.getValue() instanceof JsonNull) {
fieldValues.put(name, null);
} else if (entry.getValue() instanceof JsonArray) {
JsonArray value = (JsonArray) entry.getValue();
String[] arrValues = new String[value.size()];
for (int i = 0; i < value.size(); i++) {
arrValues[i] = value.get(i).getAsString();
}
fieldValues.put(name, arrValues);
} else if (entry.getValue() instanceof JsonObject) {
JsonObject jsonObject = ((JsonObject) entry.getValue());
String type = jsonObject.get("type").getAsString();
if ("Base64File".equals(type)) {
try {
String data = jsonObject.get("data").getAsString();
String base64 = ";base64,";
int base64Pos = data.indexOf(base64);
String mimeTypes = data.substring("data:".length(), base64Pos);
byte[] bytes = data.substring(base64Pos + base64.length(), data.length()).getBytes("UTF-8");
byte[] decoded = Base64.getDecoder().decode(bytes);
fieldValues.put(name, new Base64File(jsonObject.get("name").getAsString(), decoded, mimeTypes));
} catch (UnsupportedEncodingException e) {
throw Be5Exception.internal(e);
}
} else {
fieldValues.put(name, jsonObject.toString());
}
} else if (entry.getValue() instanceof JsonPrimitive) {
fieldValues.put(name, ((JsonPrimitive) entry.getValue()).getAsString());
}
}
return replaceEmptyStringToNull(fieldValues);
}
use of com.google.gson.JsonNull in project maple-ir by LLVM-but-worse.
the class JsonTreeReader method peek.
@Override
public JsonToken peek() throws IOException {
if (stack.isEmpty()) {
return JsonToken.END_DOCUMENT;
}
Object o = peekStack();
if (o instanceof Iterator) {
boolean isObject = stack.get(stack.size() - 2) instanceof JsonObject;
Iterator<?> iterator = (Iterator<?>) o;
if (iterator.hasNext()) {
if (isObject) {
return JsonToken.NAME;
} else {
stack.add(iterator.next());
return peek();
}
} else {
return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
}
} else if (o instanceof JsonObject) {
return JsonToken.BEGIN_OBJECT;
} else if (o instanceof JsonArray) {
return JsonToken.BEGIN_ARRAY;
} else if (o instanceof JsonPrimitive) {
JsonPrimitive primitive = (JsonPrimitive) o;
if (primitive.isString()) {
return JsonToken.STRING;
} else if (primitive.isBoolean()) {
return JsonToken.BOOLEAN;
} else if (primitive.isNumber()) {
return JsonToken.NUMBER;
} else {
throw new AssertionError();
}
} else if (o instanceof JsonNull) {
return JsonToken.NULL;
} else if (o == SENTINEL_CLOSED) {
throw new IllegalStateException("JsonReader is closed");
} else {
throw new AssertionError();
}
}
Aggregations