Skip to content

Commit a641d57

Browse files
committed
json-iterator#50 fix deserialize with byte[]
1 parent 7ddbf9f commit a641d57

2 files changed

Lines changed: 26 additions & 36 deletions

File tree

src/main/java/com/jsoniter/JsonIterator.java

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -366,47 +366,20 @@ protected JsonIterator initialValue() {
366366
};
367367

368368
public static final <T> T deserialize(String input, Class<T> clazz) {
369-
JsonIterator iter = tlsIter.get();
370-
byte[] bytes = input.getBytes();
371-
iter.reset(bytes);
372-
try {
373-
T val = iter.read(clazz);
374-
if (iter.head != bytes.length) {
375-
System.out.println(iter.head);
376-
System.out.println(new String(bytes));
377-
System.out.println(new String(bytes).substring(75));
378-
throw iter.reportError("deserialize", "trailing garbage found");
379-
}
380-
return val;
381-
} catch (ArrayIndexOutOfBoundsException e) {
382-
throw iter.reportError("deserialize", "premature end");
383-
} catch (IOException e) {
384-
throw new JsonException(e);
385-
}
369+
return deserialize(input.getBytes(), clazz);
386370
}
387371

388372
public static final <T> T deserialize(String input, TypeLiteral<T> typeLiteral) {
389-
JsonIterator iter = tlsIter.get();
390-
iter.reset(input.getBytes());
391-
try {
392-
T val = iter.read(typeLiteral);
393-
if (IterImpl.nextToken(iter) != 0) {
394-
throw iter.reportError("deserialize", "trailing garbage found");
395-
}
396-
return val;
397-
} catch (ArrayIndexOutOfBoundsException e) {
398-
throw iter.reportError("deserialize", "premature end");
399-
} catch (IOException e) {
400-
throw new JsonException(e);
401-
}
373+
return deserialize(input.getBytes(), typeLiteral);
402374
}
403375

404376
public static final <T> T deserialize(byte[] input, Class<T> clazz) {
377+
int lastNotSpacePos = findLastNotSpacePos(input);
405378
JsonIterator iter = tlsIter.get();
406-
iter.reset(input);
379+
iter.reset(input, 0, lastNotSpacePos);
407380
try {
408381
T val = iter.read(clazz);
409-
if (IterImpl.nextToken(iter) != 0) {
382+
if (iter.head != lastNotSpacePos) {
410383
throw iter.reportError("deserialize", "trailing garbage found");
411384
}
412385
return val;
@@ -418,11 +391,12 @@ public static final <T> T deserialize(byte[] input, Class<T> clazz) {
418391
}
419392

420393
public static final <T> T deserialize(byte[] input, TypeLiteral<T> typeLiteral) {
394+
int lastNotSpacePos = findLastNotSpacePos(input);
421395
JsonIterator iter = tlsIter.get();
422-
iter.reset(input);
396+
iter.reset(input, 0, lastNotSpacePos);
423397
try {
424398
T val = iter.read(typeLiteral);
425-
if (IterImpl.nextToken(iter) != 0) {
399+
if (iter.head != lastNotSpacePos) {
426400
throw iter.reportError("deserialize", "trailing garbage found");
427401
}
428402
return val;
@@ -438,11 +412,12 @@ public static final Any deserialize(String input) {
438412
}
439413

440414
public static final Any deserialize(byte[] input) {
415+
int lastNotSpacePos = findLastNotSpacePos(input);
441416
JsonIterator iter = tlsIter.get();
442-
iter.reset(input);
417+
iter.reset(input, 0, lastNotSpacePos);
443418
try {
444419
Any val = iter.readAny();
445-
if (iter.head != input.length) {
420+
if (iter.head != lastNotSpacePos) {
446421
throw iter.reportError("deserialize", "trailing garbage found");
447422
}
448423
return val;
@@ -453,6 +428,16 @@ public static final Any deserialize(byte[] input) {
453428
}
454429
}
455430

431+
private static int findLastNotSpacePos(byte[] input) {
432+
for(int i = input.length - 1; i >= 0; i--) {
433+
byte c = input[i];
434+
if (c != ' ' && c != '\t' && c != '\n' && c != '\r') {
435+
return i + 1;
436+
}
437+
}
438+
return 0;
439+
}
440+
456441
public static void setMode(DecodingMode mode) {
457442
Codegen.setMode(mode);
458443
}

src/test/java/com/jsoniter/TestDemo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,9 @@ public void test_utf8() {
166166
TestObject obj = JsonIterator.deserialize(input, TestObject.class);
167167
assertEquals(0, obj.commentCount);
168168
}
169+
170+
public void test_deserialize() {
171+
String str = "{\"port\":13110} ";
172+
JsonIterator.deserialize(str.getBytes(), HashMap.class);
173+
}
169174
}

0 commit comments

Comments
 (0)