Skip to content

Commit

Permalink
Fix JsonReader support for unicode strings
Browse files Browse the repository at this point in the history
  • Loading branch information
afrankvt committed Jun 17, 2021
1 parent 8094051 commit 73d7035
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Version 0.3 (working)
* Add Point discovery support
* Fix JsonReader support for unicode strings

## Version 0.2 (16-Jun-2021)
* Fix JSON parsing of numbers using `E` notation
Expand Down
37 changes: 34 additions & 3 deletions novant/novant-rt/src/io/novant/util/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,30 @@ private String readStr() throws IOException
read('\"');
while (peek != '\"')
{
// TODO: yeah fix this!
if (peek == '\\') buf.append((char)read());
buf.append((char)read());
if (peek == '\\')
{
int p = read();
if (peek == 'u')
{
read();
int n3 = hex(read());
int n2 = hex(read());
int n1 = hex(read());
int n0 = hex(read());
int uc = ((n3 << 12) | (n2 << 8) | (n1 << 4) | n0);
buf.append((char)uc);
}
else
{
// TODO FIXIT!
buf.append((char)p);
buf.append((char)read());
}
}
else
{
buf.append((char)read());
}
}
read('\"');
return buf.toString();
Expand Down Expand Up @@ -171,6 +192,7 @@ private int read() throws IOException
private int read(int expected) throws IOException
{
int ch = read();
if (ch < 0) throw new IOException("Unexpected EOS");
if (ch != expected) throw unexpectedChar(ch);
return ch;
}
Expand All @@ -181,6 +203,15 @@ private void eatWhitespace() throws IOException
while (peek == ' ') read();
}

/** Convert hex char to base 10 digit */
static int hex(int c)
{
if ('0' <= c && c <= '9') return c - '0';
if ('a' <= c && c <= 'f') return c - 'a' + 10;
if ('A' <= c && c <= 'F') return c - 'A' + 10;
return -1;
}

private IOException unexpectedChar(int ch)
{
return new IOException("Unexpected char '" + ((char)ch) + "' [" + pos + "]");
Expand Down
18 changes: 12 additions & 6 deletions novant/novant-rt/srcTest/test/io/novant/BJsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public class BJsonTest extends BTestNg
@Test public void testStr() throws IOException
{
verifyEq(read("\"foo\""), "foo");
verifyEq(read("\"Unicode \\u00b0F rocks\""), "Unicode °F rocks");
verifyEq(read("\"this has some spaces\""), "this has some spaces");
// TODO: is this test right???
verifyEq(read("\"this has a \\\" quote\""), "this has a \\\" quote");
verifyEq(read("\"this has a \\\" quote\""), "this has a \\\" quote");
}

@Test public void testList() throws IOException
Expand Down Expand Up @@ -144,27 +145,32 @@ public class BJsonTest extends BTestNg
" \"name\": \"Meter A\"," +
" \"size\": 3," +
" \"points\": [" +
" { \"id\":\"p1\", \"name\": \"DI-1\", \"writable\": false }," +
" { \"id\":\"p2\", \"name\": \"DI-2\", \"writable\": false }," +
" { \"id\":\"p3\", \"name\": \"DO-1\", \"writable\": true }" +
" { \"id\":\"p1\", \"name\": \"AI-1\", \"kind\":\"num\", \"writable\": false, \"unit\":\"\\u00b0F\" }," +
" { \"id\":\"p2\", \"name\": \"AI-2\", \"kind\":\"num\", \"writable\": false, \"unit\":\"kW\" }," +
" { \"id\":\"p3\", \"name\": \"DO-1\", \"kind\":\"bool\", \"writable\": true }" +
" ]" +
" }" +
"]" +
"}";

HashMap p1 = new HashMap();
p1.put("id", "p1");
p1.put("name", "DI-1");
p1.put("name", "AI-1");
p1.put("kind", "num");
p1.put("writable", new Boolean(false));
p1.put("unit", "°F");

HashMap p2 = new HashMap();
p2.put("id", "p2");
p2.put("name", "DI-2");
p2.put("name", "AI-2");
p2.put("kind", "num");
p2.put("writable", new Boolean(false));
p2.put("unit", "kW");

HashMap p3 = new HashMap();
p3.put("id", "p3");
p3.put("name", "DO-1");
p3.put("kind", "bool");
p3.put("writable", new Boolean(true));

ArrayList points = new ArrayList();
Expand Down

0 comments on commit 73d7035

Please sign in to comment.