Skip to content

Commit

Permalink
3.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
miktim committed Oct 17, 2024
1 parent 0096ba1 commit 0b0641d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
Binary file renamed dist/json-3.0.0.jar → dist/json-3.0.1.jar
Binary file not shown.
7 changes: 6 additions & 1 deletion src/org/miktim/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,10 @@ public <T> T castMember(Class<T> cls, String memberName, int... indices) {
public Json normalize() throws IOException, ParseException {
return (Json) JSON.fromJSON(this.toString()); // :)
}


public static class JsonConverter extends JsonObject {
public JsonConverter() {

}}
public static JsonConverter converter = new JsonConverter();
}
2 changes: 1 addition & 1 deletion src/org/miktim/json/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void onError(String name, Exception e) {
*/

@SuppressWarnings("unchecked")
protected <T> T castMember(String memberName, Json jsonObj, T sample) {
protected <T> T castMember(T sample, String memberName, Json jsonObj) {
if (jsonObj.get(memberName) != null) {
return JSON.cast(jsonObj.get(memberName), sample);
}
Expand Down
30 changes: 15 additions & 15 deletions test/json/JsonObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.HashMap;
import java.util.Map;
import org.miktim.json.Json;
import org.miktim.json.JsonConverter;
import org.miktim.json.JsonObject;

public class JsonObjectTest {
Expand Down Expand Up @@ -76,7 +75,7 @@ static class D {

protected String pro_dS = "protected dS";
public int pub_di = 123;
String dS = "";
String def_S = "";
public C pub_dC = new C();
private String pri_dS;

Expand All @@ -85,7 +84,7 @@ static class D {

D(int i, String s) {
pub_di = i;
dS = s;
def_S = s;
}
}

Expand Down Expand Up @@ -116,7 +115,7 @@ class B extends A {

@Override
protected Object replacer(String name, Object value) {
log(name);
// log(name);
try {
if (isClassName(name)) { // first call
// ignored field was declared in A
Expand All @@ -138,7 +137,7 @@ protected Object reviver(String name, Object value) {
try {
if (isClassName(name)) {
// load a.priv_ad by setter
set_ad(castMember("ad", (Json) value, get_ad()));
set_ad(castMember(get_ad(),"ad", (Json) value));
} else if (name.endsWith(".bD")) {
return fromJson(bD, (Json) value);
}
Expand Down Expand Up @@ -232,27 +231,28 @@ public static void main(String[] args) throws Exception {
t.av.enableNames = false;
t.av.fromJson(j);
log(t.av.toJson());

log(" Av instance with Json.converter");
log(Json.converter.toJson(t.av));

log("\n\r B instance toJson/fromJson :");
String s = t.b.toJson().toJSON();
log(s);
t.b.fromJson(new Json(s));

JsonConverter converter = new JsonConverter();
log("\n\r D instance with default converter:");
j = converter.toJson(t.d);
log("\n\r D instance with Json.converter:");
j = Json.converter.toJson(t.d);
log(j);
log(" load updated Json into new D instance");
j.set("pro_dS", "updated dS").set("pub_di", 0);
t.d = converter.fromJson(new D(), j);
log(converter.toJson(t.d));
t.d = Json.converter.fromJson(new D(), j);
log(Json.converter.toJson(t.d));

log("\n\r B instance with default converter:");
j = converter.toJson(t.b);
log("\n\r B instance with Json.converter:");
j = Json.converter.toJson(t.b);
log(j);

log("\n\r java.lang.reflect.Modifier instance with default converter:");
j = converter.toJson(new Modifier());
log("\n\r java.lang.reflect.Modifier instance with Json.converter:");
j = Json.converter.toJson(new Modifier());
log(j);

}
Expand Down

0 comments on commit 0b0641d

Please sign in to comment.