Skip to content

Commit

Permalink
Fixing Json instantiation using reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
ajermakovics committed Mar 3, 2017
1 parent bc42584 commit ff1fce1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/java/org/andrejs/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public int hashCode() {

@SuppressWarnings("unchecked")
public <T> T get(String key) {
return (T) map.get(key);
return (T) toMap().get(key);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand Down Expand Up @@ -146,8 +146,10 @@ public Json copy() {

/** Read declared field values using reflection **/
Map<String, Object> readFields() {
Map<String, Object> fieldVals = new LinkedHashMap<String, Object>();
Map<String, Object> fieldVals = new LinkedHashMap<>();
for(Field f: getClass().getDeclaredFields()) {
if(f.getName().contains("$"))
continue;
Object val = JsonSerializer.getFieldValue(f, this);
if( val != null && !isStatic(f.getModifiers()) )
fieldVals.put( f.getName() , val);
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/andrejs/json/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void containsAnonymousClassFields() throws Exception {
String key2 = "val";
};
int val = json.get("key");
System.out.println(json);
assertEquals(123, val);
assertEquals("val", json.get("key2"));
}
Expand Down

0 comments on commit ff1fce1

Please sign in to comment.