Commit 1f3d1814 authored by Emeric Verschuur's avatar Emeric Verschuur

JSON Serialization

parent f8a7125f
......@@ -31,11 +31,6 @@ public class KeyNotFoundException extends RuntimeException {
super();
}
public KeyNotFoundException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public KeyNotFoundException(String message, Throwable cause) {
super(message, cause);
}
......
......@@ -18,6 +18,7 @@ package org.mbedsys.jvar;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
......@@ -70,6 +71,25 @@ public abstract class Variant implements Comparable<Object> {
};
public static int JSON_INDENT_MASK = 0x0000001F;
private static char[] spaces = null;
public static void appendSpaces(StringBuffer buffer, int count) {
if (spaces == null || spaces.length < count) {
spaces = new char[count + 32];
Arrays.fill(spaces, ' ');
}
buffer.append(spaces, 0, count);
}
public static void appendSpaces(OutputStreamWriter writer, int count)
throws IOException {
if (spaces == null || spaces.length < count) {
spaces = new char[count + 32];
Arrays.fill(spaces, ' ');
}
writer.write(spaces, 0, count);
}
/**
* Returns the variant as a boolean.
......@@ -332,7 +352,7 @@ public abstract class Variant implements Comparable<Object> {
* @param writer output stream writer
* @throws IOException
*/
public static void serializeJSONMap(OutputStreamWriter writer, Map<String, Variant> map, int flags) throws IOException {
private static void serializeJSONMap(OutputStreamWriter writer, Map<String, Variant> map, int flags) throws IOException {
Iterator<String> keys = map.keySet().iterator();
boolean conpact = (flags & FORMAT_JSON_COMPACT) != 0;
int indentStep = flags & JSON_INDENT_MASK;
......@@ -376,25 +396,55 @@ public abstract class Variant implements Comparable<Object> {
writer.write('}');
}
protected static void appendSpaces(OutputStreamWriter writer, int cout) {
// TODO Auto-generated method stub
}
protected static void serializeJSONElt(OutputStreamWriter writer,
Variant next, int flags) {
// TODO Auto-generated method stub
Variant variant, int flags) throws IOException {
switch (variant.type()) {
case BOOL:
writer.append(variant.booleanValue()?"true":"false");
break;
case BYTE:
writer.append(Byte.toString(variant.byteValue()));
break;
case BYTEARRAY:
case STRING:
case DATETIME:
VariantString.writeJSONTo(writer, variant.toString());
break;
case LIST:
serializeJSONList(writer, variant.toList(), flags);
break;
case MAP:
serializeJSONMap(writer, variant.toMap(), flags);
break;
case NULL:
writer.append("null");
break;
case INT:
case UINT:
case LONG:
case ULONG:
writer.append(Long.toString(variant.longValue()));
break;
case DOUBLE:
writer.append(Double.toString(variant.doubleValue()));
break;
default:
break;
}
}
protected static void serializeJSON(OutputStreamWriter writer,
VariantList variantList, int i) {
// TODO Auto-generated method stub
}
protected static String[] split(String path, char sep) {
// TODO Auto-generated method stub
return null;
Variant variant, int flags) throws IOException {
switch (variant.type()) {
case LIST:
serializeJSONList(writer, variant.toList(), flags);
break;
case MAP:
serializeJSONMap(writer, variant.toMap(), flags);
break;
default:
throw new IllegalArgumentException("The root node to serialize must be an map or an array");
}
}
}
......@@ -96,7 +96,7 @@ public class VariantMap extends Variant implements Map<String, Variant> {
}
public static VariantMap resolvePath(VariantMap data, String path, char sep) {
for (String key: split(path, sep)) {
for (String key: VariantString.split(path, sep)) {
if (!data.containsKey(key)) {
return null;
}
......@@ -131,7 +131,7 @@ public class VariantMap extends Variant implements Map<String, Variant> {
}
public static Variant updatePath(Variant data, String path, Variant value) {
return updatePath(data, split(path, '.'), value);
return updatePath(data, VariantString.split(path, '.'), value);
}
@Override
......
......@@ -18,6 +18,7 @@ package org.mbedsys.jvar;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
/**
*
......@@ -153,4 +154,74 @@ public class VariantString extends Variant {
public boolean isNull() {
return false;
}
public static String toCamelCase(String s, boolean firstCharToUpperCase) {
StringBuffer result = new StringBuffer();
String ref = s.toLowerCase();
if (ref.length() != 0) {
if (firstCharToUpperCase) {
result.append(Character.toUpperCase(ref.charAt(0)));
} else {
result.append(ref.charAt(0));
}
}
boolean uppercase = false;
for (int i = 1; i < ref.length(); i++) {
char c = ref.charAt(i);
if (c == '_') {
uppercase = true;
continue;
}
if (uppercase) {
result.append(Character.toUpperCase(ref.charAt(i)));
uppercase = false;
} else {
result.append(ref.charAt(i));
}
}
return result.toString();
}
public static String escape(String s) {
StringBuffer result = new StringBuffer();
char c;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
switch (c) {
case '\\':
case '"':
case '\b':
case '\f':
case '\n':
case '\r':
case '\t':
result.append('\\');
default:
result.append(c);
break;
}
}
return result.toString();
}
public static String[] split(String src, char sep) {
StringTokenizer st = new StringTokenizer(src, "" + sep);
String[] ret = new String[st.countTokens()];
int i = 0;
for (i = 0; i < ret.length; i++) {
ret[i] = st.nextToken();
}
return ret;
}
public static String[] split(String src, String sep) {
StringTokenizer st = new StringTokenizer(src, sep);
String[] ret = new String[st.countTokens()];
int i = 0;
for (i = 0; i < ret.length; i++) {
ret[i] = st.nextToken();
}
return ret;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment