Skip to content

Commit

Permalink
Added option to read json string as char buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
ingon committed Feb 27, 2020
1 parent d679c23 commit 9efcf81
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id 'java-library'
}

version = '1.0.0'
version = '1.0.1'
group = 'dev.ingon'

repositories {
Expand Down
83 changes: 44 additions & 39 deletions src/main/java/dev/ingon/json/zero/ContentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,57 @@ public interface ContentHandler {
boolean endArray() throws ParseException;

default String readString(char[] source, int begin, int end, int escapeCount) {
return readChars(source, begin, end, escapeCount).toString();
}

default CharBuffer readChars(char[] source, int begin, int end, int escapeCount) {
if (escapeCount == 0) {
return String.valueOf(source, begin, end - begin);
} else {
StringBuilder sb = new StringBuilder(end - begin);
for (int i = begin; i < end; i++) {
char ch = source[i];
return CharBuffer.wrap(source, begin, end - begin);
}

CharBuffer cb = CharBuffer.allocate(end - begin);
for (int i = begin; i < end; i++) {
char ch = source[i];
switch (ch) {
case '\\':
ch = source[++i];
switch (ch) {
case '\\':
ch = source[++i];
switch (ch) {
case '"':
case '\\':
case '/':
sb.append(ch);
break;
case 'b':
sb.append('\b');
break;
case 'f':
sb.append('\f');
break;
case 'n':
sb.append('\n');
break;
case 'r':
sb.append('\r');
break;
case 't':
sb.append('\t');
break;
case 'u':
int codePoint = Integer.parseInt(CharBuffer.wrap(source, i + 1, 4), 0, 4, 16);
sb.append(Character.toChars(codePoint));
i += 4;
}
break;
case '"':
throw new RuntimeException("wat");
default:
sb.append(ch);
case '\\':
case '/':
cb.put(ch);
break;
case 'b':
cb.put('\b');
break;
case 'f':
cb.put('\f');
break;
case 'n':
cb.put('\n');
break;
case 'r':
cb.put('\r');
break;
case 't':
cb.put('\t');
break;
case 'u':
int codePoint = Integer.parseInt(CharBuffer.wrap(source, i + 1, 4), 0, 4, 16);
cb.put(Character.toChars(codePoint));
i += 4;
}
break;
case '"':
throw new RuntimeException("wat");
default:
cb.put(ch);
}
return sb.toString();
}

return cb.flip();
}

default long readLong(char[] source, int begin, int end) {
return Long.parseLong(new String(source, begin, end - begin));
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/dev/ingon/json/zero/ParserPositiveTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void testStrings() throws ParseException {
assertPass("\"abc\"", ">>> ", "S:abc", " <<<");

// harder
assertPass("\"\\t\\b\"", ">>> ", "S:\t\b", " <<<");
assertPass("\"\\\" \\\\ \\/ \\b \\f \\n \\r \\t\"", ">>> ", "S:\" \\ / \b \f \n \r \t", " <<<");
assertPass("\"\\u73e1\\u52ab\"", ">>> ", "S:\u73e1\u52ab", " <<<");
}

Expand Down

0 comments on commit 9efcf81

Please sign in to comment.