Skip to content

Commit

Permalink
3.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
miktim committed Dec 25, 2024
1 parent b8be32f commit 84b6542
Show file tree
Hide file tree
Showing 12 changed files with 654 additions and 547 deletions.
345 changes: 217 additions & 128 deletions README.md

Large diffs are not rendered by default.

9 changes: 0 additions & 9 deletions src/org/miktim/json/AbstractNaturalizer.java

This file was deleted.

75 changes: 25 additions & 50 deletions src/org/miktim/json/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,9 @@
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.text.ParseException;
import java.util.HashMap;

public class JSON {

/*
static {
try {
} catch (Exception e) {
}
}
*/
private static HashMap<Class, JsonObject> serializers = new HashMap<>();

public static JsonObject registerClass(Class cls, JsonObject serializer) {
return serializers.put(cls, serializer);
}

public static JsonObject getRegistered(Class cls) {
return serializers.get(cls);
}

public static Class[] listRegistered() {
return serializers.keySet().toArray(new Class[0]);
}

public static JsonObject unregisterClass(Class cls) {
return serializers.remove(cls);
}

public static <T> T toJSON(T obj, OutputStream out, int space, String charsetName)
throws IOException {
JSONGenerator generator = new JSONGenerator(out, space, charsetName);
Expand Down Expand Up @@ -110,14 +82,10 @@ public static <T> T cast(Class<T> dstCls, Object obj) {

@SuppressWarnings({"unchecked"})
static <T> T cast(Class<T> dstCls, Object obj, CastAdapter adapter) {
// if (srcCls.isArray()) { //*
// if (obj.getClass().isArray()){
if (dstCls.isArray()) {
int arrLen = obj == null ? 0 : Array.getLength(obj);
// Class cmpCls = srcCls.getComponentType(); //*
Class cmpDstCls = dstCls.getComponentType();
T arr = (T) Array.newInstance(cmpDstCls, arrLen);
// T arr = (T) Array.newInstance(cmpCls, arrLen); //*
for (int i = 0; i < arrLen; i++) {
T retVal = (T) cast(cmpDstCls, Array.get(obj, i), adapter);
Array.set(arr, i, retVal);
Expand All @@ -144,7 +112,11 @@ public static boolean isNativeType(Object obj) {
if (obj == null) {
return true;
}
Class<?> cls = JSON.getElementClass(obj.getClass());
return isNativeClass(obj.getClass());
}

public static boolean isNativeClass(Class clazz) {
Class<?> cls = JSON.getElementClass(clazz);
return cls.equals(Number.class)
|| cls.equals(String.class)
|| cls.equals(Boolean.class)
Expand All @@ -157,7 +129,7 @@ interface CastAdapter {

<T> T castValue(Object value) throws ClassCastException;
}
// TODO: safeCast from BigDecimal?
// TODO: ?safeCast from BigDecimal

@SuppressWarnings("unchecked")
static CastAdapter getAdapter(Class cls) {
Expand All @@ -182,38 +154,41 @@ static CastAdapter getAdapter(Class cls) {
return booleanAdapter;
} else if (cls == Json.class) {
return jsonAdapter;
} else if (cls.isAssignableFrom(JsonObject.class)) {
return new JsonObjectAdapter(cls);
// } else if (JsonConvertible.class.isAssignableFrom(cls)) {
// return new ObjectAdapter(cls);
}

return defaultAdapter;
// TODO: ?Map, List adapters
return new ObjectAdapter(cls);
// return defaultAdapter;
}
/*
@SuppressWarnings("unchecked")
private static final CastAdapter defaultAdapter = new CastAdapter() {
@Override
public Object castValue(Object obj) {
return obj;
}
};

*/
@SuppressWarnings("unchecked")
static class JsonObjectAdapter implements CastAdapter {
static class ObjectAdapter implements CastAdapter {

Class<?> clazz;

JsonObjectAdapter(Class<?> clazz) {
ObjectAdapter(Class<?> clazz) {
this.clazz = clazz;
}

@Override
public Object castValue(Object json) {
public Object castValue(Object obj) {
try {
if (json == null) {
return null;
if (obj == null || obj.getClass() == clazz) {
return obj;
}
Object obj = createInstance(clazz);
return ((JsonObject) obj).fromJson(json);
} catch (IllegalArgumentException | IllegalAccessException ex) {
// if(obj.getClass() == clazz) return obj;
Object newObj = createInstance(clazz);
return Json.converter.fromJson(newObj, (Json)obj);
} catch (IllegalArgumentException ex) {
throw new ClassCastException(ex.getMessage());
}
}
Expand All @@ -223,8 +198,8 @@ public Object castValue(Object json) {
static Object createInstance(Class<?> clazz) {
try {
// Class<?> clazz = obj.getClass();
Constructor<?> ctor = clazz.getConstructor(String.class);
return ctor.newInstance(new Object[0]);
Constructor<?> ctor = clazz.getConstructor();
return ctor.newInstance();//new Object[0]);
} catch (Exception ex) {
throw new ClassCastException(ex.getMessage());
}
Expand Down Expand Up @@ -268,7 +243,7 @@ public Object castValue(Object obj) {
private static final CastAdapter longAdapter = new CastAdapter() {
@Override
public Object castValue(Object obj) {
return obj == null ? 0 : ((Number) obj).longValue();
return obj == null ? 0L : ((Number) obj).longValue();
}
};
@SuppressWarnings("unchecked")
Expand Down
6 changes: 4 additions & 2 deletions src/org/miktim/json/JSONGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ void generateObject(Object value, int level) throws IOException {
} else if (value instanceof String) {
write("\"" + escapeString((String) value) + "\"");
return;
} else if (value instanceof Character) {
generateObject(String.valueOf(value), level - 1);
return;
} else if (value.getClass().isArray()) {
write("[");
String separator = "";
Expand Down Expand Up @@ -83,10 +86,9 @@ void generateObject(Object value, int level) throws IOException {
return;
}
try {
Json.converter.toJson(value);
generateObject(Json.converter.toJson(value), level);
} catch (Exception ex) {
throw new ClassCastException(ex.getMessage());
// generateObject(ex, level);
}
}

Expand Down
26 changes: 25 additions & 1 deletion src/org/miktim/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,35 @@ public Json normalize() throws IOException, ParseException {
return (Json) JSON.fromJSON(this.toString()); // :)
}
*/
public static class Converter extends JsonObject {
public static class Converter extends JsonConverter {

public Converter() {

}

@SuppressWarnings("unchecked")
public Json toJson(Object targetObj) {
// throws IllegalArgumentException, IllegalAccessException {
return unload(this, targetObj);
}

@SuppressWarnings("unchecked")
public <T> T fromJson(Object targetObj, Object jsonObj) {
// throws IllegalArgumentException, IllegalAccessException {
return (T) load(this, targetObj, jsonObj);
}
/*
@Override
public Object replacer(String name, Object value) {
return value;
}
@Override
public Object reviver(String name, Object value) {
return value;
}
*/
}

public static Converter converter = new Converter();
}
Loading

0 comments on commit 84b6542

Please sign in to comment.