-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added better adapters, removed ansi dep
- Loading branch information
1 parent
f7cb7b0
commit 518fa01
Showing
9 changed files
with
179 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/dev/latvian/apps/json/adapter/CollectionJSONAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package dev.latvian.apps.json.adapter; | ||
|
||
import dev.latvian.apps.json.JSON; | ||
import dev.latvian.apps.json.JSONArray; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
import java.util.LinkedList; | ||
|
||
public class CollectionJSONAdapter implements JSONAdapter<Iterable<?>> { | ||
public interface Factory { | ||
Factory LIST = ArrayList::new; | ||
Factory LINKED_LIST = s -> new LinkedList<>(); | ||
Factory LINKED_SET = LinkedHashSet::new; | ||
|
||
Collection<?> create(int size); | ||
} | ||
|
||
private final Class<?> type; | ||
private final Factory factory; | ||
|
||
CollectionJSONAdapter(Class<?> type, Factory factory) { | ||
this.type = type; | ||
this.factory = factory; | ||
} | ||
|
||
@Override | ||
public Iterable<?> adapt(JSON json, Object jsonValue, Type genericType) { | ||
if (jsonValue instanceof JSONArray jsonArray) { | ||
var collection = factory.create(jsonArray.size()); | ||
var valueType = genericType instanceof ParameterizedType pt ? pt.getActualTypeArguments()[0] : null; | ||
|
||
for (var value : jsonArray) { | ||
collection.add(json.adapt(value, valueType)); | ||
} | ||
|
||
return collection; | ||
} | ||
|
||
throw new IllegalArgumentException("Expected JSON array for type '" + type.getName() + "'"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/dev/latvian/apps/json/adapter/MapJSONAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package dev.latvian.apps.json.adapter; | ||
|
||
import dev.latvian.apps.json.JSON; | ||
import dev.latvian.apps.json.JSONObject; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.IdentityHashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
public class MapJSONAdapter implements JSONAdapter<Map<String, ?>> { | ||
public interface Factory { | ||
Factory MAP = LinkedHashMap::new; | ||
Factory IDENTITY_MAP = IdentityHashMap::new; | ||
|
||
Map<String, ?> create(int size); | ||
} | ||
|
||
private final Class<?> type; | ||
private final Factory factory; | ||
|
||
MapJSONAdapter(Class<?> type, Factory factory) { | ||
this.type = type; | ||
this.factory = factory; | ||
} | ||
|
||
@Override | ||
public Map<String, ?> adapt(JSON json, Object jsonValue, Type genericType) { | ||
if (jsonValue instanceof JSONObject jsonObject) { | ||
var map = factory.create(jsonObject.size()); | ||
var valueType = genericType instanceof ParameterizedType pt ? pt.getActualTypeArguments()[1] : null; | ||
|
||
for (var entry : jsonObject.entrySet()) { | ||
map.put(entry.getKey(), json.adapt(entry.getValue(), valueType)); | ||
} | ||
|
||
return map; | ||
} | ||
|
||
throw new IllegalArgumentException("Expected JSON object for type '" + type.getName() + "'"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.