Skip to content

Commit

Permalink
Library Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Bestsoft101 committed Nov 24, 2024
1 parent 7e4d368 commit 72d5758
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
28 changes: 24 additions & 4 deletions src/b100/json/element/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}

Expand Down Expand Up @@ -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;
}
Expand Down
27 changes: 24 additions & 3 deletions src/b100/json/element/JsonString.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down
13 changes: 12 additions & 1 deletion src/b100/utils/InvalidCharacterException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions src/b100/utils/StringReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand All @@ -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();
Expand Down

0 comments on commit 72d5758

Please sign in to comment.