json.com.unbound.common.JSON Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unbound-java-provider Show documentation
Show all versions of unbound-java-provider Show documentation
This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi
package com.unbound.common;
import java.util.*;
public final class JSON
{
public static Object convert(String in)
{
Parser parser = new Parser(in);
Object item = parser.parseAny();
Parser.check(parser.token==0);
return item;
}
public static String convert(Object in)
{
StringBuilder sb = new StringBuilder();
build(in, sb);
return sb.toString();
}
private static void build(Object in, StringBuilder sb)
{
if (in==null) sb.append("null");
else if (in instanceof List) buildList((List)in, sb);
else if (in instanceof Map) buildMap((Map)in, sb);
else if (in instanceof Boolean) sb.append(((Boolean)in).booleanValue() ? "true" : "false");
else if (in instanceof Long) sb.append(((Long)in).toString());
else if (in instanceof Integer) sb.append(((Integer)in).toString());
else if (in instanceof Short) sb.append(((Short)in).toString());
else if (in instanceof Byte) sb.append(((Byte)in).toString());
else if (in instanceof Double) sb.append(((Double)in).toString());
else if (in instanceof Float) sb.append(((Float)in).toString());
else if (in instanceof String) buildString((String)in, sb);
}
private static void buildString(String value, StringBuilder sb)
{
sb.append('"');
int length = value.length();
for (int i=0; i>8) & 0x0f]);
sb.append(HEX.upChars[c & 0x0f]);
}
else
sb.append(c);
}
}
sb.append('"');
}
private static void buildMap(Map map, StringBuilder sb)
{
sb.append('{');
boolean first = true;
for (Map.Entry entry : map.entrySet())
{
if (!first)
{
sb.append(',');
first = false;
}
sb.append('"');
java.lang.String key = entry.getKey();
sb.append(key, 0, key.length());
sb.append('"');
sb.append(':');
build(entry.getValue(),sb);
}
sb.append('}');
}
private static void buildList(List