Add constructor to JsonArray & JsonMap for bettr developer experience
This commit is contained in:
parent
12f2561921
commit
cdafc9cbaf
3 changed files with 28 additions and 13 deletions
|
@ -3,16 +3,25 @@ package dev.peerat.framework.utils.json;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class JsonArray extends Json{
|
||||
|
||||
private List<Object> list;
|
||||
private Collection<Object> list;
|
||||
|
||||
public JsonArray(){
|
||||
this.list = new ArrayList<>();
|
||||
}
|
||||
|
||||
public JsonArray(Collection<Object> collection){
|
||||
this();
|
||||
this.list.addAll(collection);
|
||||
}
|
||||
|
||||
public JsonArray(Iterator<Object> iterator){
|
||||
this();
|
||||
while(iterator.hasNext()) this.list.add(iterator.next());
|
||||
}
|
||||
|
||||
public <E> Collection<E> toList(){
|
||||
return (Collection<E>) this.list;
|
||||
}
|
||||
|
|
|
@ -14,6 +14,11 @@ public class JsonMap extends Json{
|
|||
this.map = new HashMap<>();
|
||||
}
|
||||
|
||||
public JsonMap(Map<String, Object> map){
|
||||
this();
|
||||
for(Entry<String, Object> entry : map.entrySet()) this.map.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
public Set<Entry<String, Object>> entries(){
|
||||
return this.map.entrySet();
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package dev.peerat.framework.utils.json;
|
||||
|
||||
import static be.jeffcheasey88.peeratcode.parser.TokenType.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -8,7 +10,6 @@ import java.util.Map.Entry;
|
|||
|
||||
import be.jeffcheasey88.peeratcode.parser.Parser;
|
||||
import be.jeffcheasey88.peeratcode.parser.Token;
|
||||
import be.jeffcheasey88.peeratcode.parser.TokenType;
|
||||
import be.jeffcheasey88.peeratcode.parser.state.RedirectStateTree;
|
||||
import be.jeffcheasey88.peeratcode.parser.state.StateTree;
|
||||
|
||||
|
@ -43,16 +44,16 @@ public class JsonParser extends Parser<Json>{
|
|||
.then(content_array_element);
|
||||
|
||||
content.then(new RedirectStateTree<>(base, (global, local) -> global.set(local.get()))).end();
|
||||
content.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.STRING), (bag, token) -> bag.set(token))).end();
|
||||
content.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.CHAR), (bag, token) -> bag.set(token))).end();
|
||||
StateTree<Json> number = content.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME), (bag, token) -> bag.set(token)));
|
||||
content.then((validator) -> validator.validate((token) -> token.getType().equals(STRING), (bag, token) -> bag.set(token))).end();
|
||||
content.then((validator) -> validator.validate((token) -> token.getType().equals(CHAR), (bag, token) -> bag.set(token))).end();
|
||||
StateTree<Json> number = content.then((validator) -> validator.validate((token) -> token.getType().equals(NAME), (bag, token) -> bag.set(token)));
|
||||
number.end();
|
||||
number.then((validator) -> validator.validate((token) -> token.getValue().equals("."), (bag, token) -> bag.set(bag.<Token>get().concat(token))))
|
||||
.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME), (bag, token) -> bag.set(bag.<Token>get().concat(token))))
|
||||
.then((validator) -> validator.validate((token) -> token.getType().equals(NAME), (bag, token) -> bag.set(bag.<Token>get().concat(token))))
|
||||
.end();
|
||||
|
||||
StateTree<Json> mapper = new StateTree<>();
|
||||
StateTree<Json> mapper_key = mapper.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.STRING), (bag, token) -> {
|
||||
StateTree<Json> mapper_key = mapper.then((validator) -> validator.validate((token) -> token.getType().equals(STRING), (bag, token) -> {
|
||||
Map<Token, Object> map = bag.get("map");
|
||||
if(map == null){
|
||||
map = new HashMap<>();
|
||||
|
@ -105,20 +106,20 @@ public class JsonParser extends Parser<Json>{
|
|||
if(value instanceof Token){
|
||||
Token token = (Token) value;
|
||||
String content = token.getValue();
|
||||
if(token.getType().equals(TokenType.STRING)){
|
||||
if(token.getType().equals(STRING)){
|
||||
return content;
|
||||
}else if(token.getType().equals(TokenType.CHAR)){
|
||||
}else if(token.getType().equals(CHAR)){
|
||||
return content.charAt(0);
|
||||
}else{
|
||||
try{
|
||||
return Long.parseLong(content);
|
||||
}catch(Exception _){
|
||||
}catch(Exception e){
|
||||
try {
|
||||
return Double.parseDouble(content);
|
||||
}catch(Exception __){
|
||||
}catch(Exception ex){
|
||||
try{
|
||||
return Boolean.parseBoolean(content);
|
||||
}catch(Exception ___){}
|
||||
}catch(Exception exc){}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue