diff --git a/src/b100/json/element/JsonObject.java b/src/b100/json/element/JsonObject.java index ca6053c..a2df808 100644 --- a/src/b100/json/element/JsonObject.java +++ b/src/b100/json/element/JsonObject.java @@ -65,12 +65,29 @@ public void write(StringWriter writer) { int i=0; for(JsonEntry entry : entries) { - if(!isCompact()) writer.write('\n'); - else writer.write(' '); + if(!isCompact()) { + writer.write('\n'); + } else { + writer.write(' '); + } + new JsonString(entry.name).write(writer); + writer.write(": "); - entry.value.write(writer); - if(i < entries.size() - 1) writer.write(','); + + if(entry.value == null) { + throw new NullPointerException("Value of entry '" + entry.name + "' is null!"); + } + + try { + entry.value.write(writer); + }catch (Exception e) { + throw new RuntimeException("Writing entry '" + entry.name + "'", e); + } + + if(i < entries.size() - 1) { + writer.write(','); + } i++; } @@ -235,6 +252,9 @@ public boolean getBoolean(String id, boolean defaultValue) { // Setters public JsonObject set(String id, JsonElement element) { + if(element == null) { + throw new NullPointerException(); + } getOrCreateEntry(id).value = element; return this; } diff --git a/src/b100/json/element/JsonString.java b/src/b100/json/element/JsonString.java index ab9d9cc..44896e4 100644 --- a/src/b100/json/element/JsonString.java +++ b/src/b100/json/element/JsonString.java @@ -4,7 +4,7 @@ import b100.utils.StringReader; import b100.utils.StringWriter; -public class JsonString implements JsonElement{ +public class JsonString implements JsonElement { public String value; @@ -25,10 +25,31 @@ public JsonString(StringReader reader) { reader.next(); char next = reader.get(); if(next == 'n' || next == 'N') { + // New line builder.append('\n'); - } else if(next == '\\'){ + + } else if(next == 'u' || next == 'U') { + // Unicode hex + reader.next(); + + String hex = reader.get(4); + int charIndex; + + try { + charIndex = Integer.parseInt(hex, 16); + }catch (NumberFormatException e) { + throw new InvalidCharacterException(reader, "Invalid hex code \"" + hex + "\""); + } + + builder.append((char) charIndex); + reader.skip(4); + + continue; + } else if(next == '\\') { + // Backslash builder.append('\\'); - }else { + + } else { throw new InvalidCharacterException(reader); } reader.next(); diff --git a/src/b100/utils/InvalidCharacterException.java b/src/b100/utils/InvalidCharacterException.java index d12e366..3da2e9c 100644 --- a/src/b100/utils/InvalidCharacterException.java +++ b/src/b100/utils/InvalidCharacterException.java @@ -9,11 +9,18 @@ public class InvalidCharacterException extends RuntimeException{ private StringReader reader; + private String message; + private int line = 1; private int column = 1; public InvalidCharacterException(StringReader stringReader) { + this(stringReader, null); + } + + public InvalidCharacterException(StringReader stringReader, String message) { this.reader = stringReader; + this.message = message; String string = reader.string(); for(int i=0; i < reader.position(); i++) { @@ -60,7 +67,11 @@ public String getLinePreview() { } public String getMessage() { - return "Invalid character \""+getPrintChar(reader.get(), false)+"\" at line "+line+" column "+column+" (index "+reader.position()+")"; + if(message != null) { + return message + " at line " + line + " column " + column + " (index " + reader.position() + ")"; + }else { + return "Invalid character \"" + getPrintChar(reader.get(), false) + "\" at line " + line + " column " + column + " (index " + reader.position() + ")"; + } } public static String getPrintChar(char c, boolean a) { diff --git a/src/b100/utils/StringReader.java b/src/b100/utils/StringReader.java index 74da544..0e2d3b1 100644 --- a/src/b100/utils/StringReader.java +++ b/src/b100/utils/StringReader.java @@ -18,6 +18,10 @@ public char get() { return string.charAt(i); } + public String get(int count) { + return string.substring(i, i + count); + } + public void skipWhitespace() { while(i < string.length() && isWhitespace(get())) i++; } @@ -28,6 +32,12 @@ public char getAndSkip() { return c; } + public String getAndSkip(int count) { + String str = get(count); + i += count; + return str; + } + public void expectAndSkip(char c) { expect(c); next();