Fix String with special char in Json
This commit is contained in:
parent
c8796e320f
commit
06ac691747
4 changed files with 64 additions and 16 deletions
|
@ -1,3 +1,50 @@
|
|||
package dev.peerat.framework.utils.json;
|
||||
|
||||
public interface Json{}
|
||||
public class Json{
|
||||
|
||||
public void buildValue(StringBuilder builder, Object value){
|
||||
if(value == null) builder.append("null");
|
||||
else if(value instanceof String){
|
||||
builder.append('"');
|
||||
for(char c : ((String)value).toCharArray()){
|
||||
if(c == '"'){
|
||||
builder.append("\\\"");
|
||||
continue;
|
||||
}
|
||||
if(c == '\\'){
|
||||
builder.append("\\\\");
|
||||
continue;
|
||||
}
|
||||
if(c == '\n'){
|
||||
builder.append("\\n");
|
||||
continue;
|
||||
}
|
||||
if(c == '\b'){
|
||||
builder.append("\\b");
|
||||
continue;
|
||||
}
|
||||
if(c == '\f'){
|
||||
builder.append("\\f");
|
||||
continue;
|
||||
}
|
||||
if(c == '\r'){
|
||||
builder.append("\\r");
|
||||
continue;
|
||||
}
|
||||
if(c == '\t'){
|
||||
builder.append("\\t");
|
||||
continue;
|
||||
}
|
||||
builder.append(c);
|
||||
}
|
||||
builder.append('"');
|
||||
}else if(value instanceof Character){
|
||||
builder.append('\'');
|
||||
builder.append(value);
|
||||
builder.append('\'');
|
||||
}else{
|
||||
builder.append(value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
package dev.peerat.framework.utils.json;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class JsonArray implements Json{
|
||||
public class JsonArray extends Json{
|
||||
|
||||
private List<Object> list;
|
||||
|
||||
|
@ -17,7 +18,16 @@ public class JsonArray implements Json{
|
|||
|
||||
@Override
|
||||
public String toString(){
|
||||
return list.toString();
|
||||
Iterator<Object> iterator = list.iterator();
|
||||
if(!iterator.hasNext()) return "[]";
|
||||
StringBuilder builder= new StringBuilder();
|
||||
builder.append('[');
|
||||
while(iterator.hasNext()){
|
||||
buildValue(builder, iterator.next());
|
||||
if(iterator.hasNext()) builder.append(',');
|
||||
}
|
||||
builder.append(']');
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ import java.util.Iterator;
|
|||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class JsonMap implements Json{
|
||||
public class JsonMap extends Json{
|
||||
|
||||
private Map<String, Object> map;
|
||||
|
||||
|
@ -38,17 +38,8 @@ public class JsonMap implements Json{
|
|||
builder.append("\":");
|
||||
|
||||
Object value = entry.getValue();
|
||||
if(value instanceof String){
|
||||
builder.append('"');
|
||||
builder.append(value);
|
||||
builder.append('"');
|
||||
}else if(value instanceof Character){
|
||||
builder.append('\'');
|
||||
builder.append(value);
|
||||
builder.append('\'');
|
||||
}else{
|
||||
builder.append(value);
|
||||
}
|
||||
buildValue(builder, value);
|
||||
|
||||
if(iterator.hasNext()) builder.append(',');
|
||||
}
|
||||
builder.append('}');
|
||||
|
|
|
@ -133,7 +133,7 @@ public class JsonParser extends Parser<Json>{
|
|||
return container.getValue();
|
||||
}
|
||||
|
||||
private static class JsonContainer implements Json{
|
||||
private static class JsonContainer extends Json{
|
||||
|
||||
private Json value;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue