Java源码示例:com.jsoniter.spi.JsonException
示例1
public LazyIterator() {
if (cache == null) {
cache = new HashMap<String, Any>();
}
mapIter = new HashMap<String, Any>(cache).entrySet().iterator();
try {
if (lastParsedPos == head) {
JsonIterator iter = JsonIteratorPool.borrowJsonIterator();
try {
iter.reset(data, lastParsedPos, tail);
if (!CodegenAccess.readObjectStart(iter)) {
lastParsedPos = tail;
} else {
lastParsedPos = CodegenAccess.head(iter);
}
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
} catch (IOException e) {
throw new JsonException(e);
}
}
示例2
public static synchronized void enable() {
if (enabled) {
throw new JsonException("Base64Support.enable can only be called once");
}
enabled = true;
JsoniterSpi.registerTypeDecoder(byte[].class, new Decoder() {
@Override
public Object decode(JsonIterator iter) throws IOException {
Slice slice = iter.readStringAsSlice();
return Base64.decodeFast(slice.data(), slice.head(), slice.tail());
}
});
JsoniterSpi.registerTypeEncoder(byte[].class, new Encoder() {
@Override
public void encode(Object obj, JsonStream stream) throws IOException {
byte[] bytes = (byte[]) obj;
stream.write('"');
Base64.encodeToBytes(bytes, stream);
stream.write('"');
}
});
}
示例3
@Category(StreamingCategory.class)
public void test_skip_string_streaming() throws IOException {
JsonIterator iter = JsonIterator.parse(new ByteArrayInputStream("\"hello".getBytes()), 2);
try {
iter.skip();
fail();
} catch (JsonException e) {
}
iter = JsonIterator.parse(new ByteArrayInputStream("\"hello\"".getBytes()), 2);
iter.skip();
iter = JsonIterator.parse(new ByteArrayInputStream("\"hello\"1".getBytes()), 2);
iter.skip();
assertEquals(1, iter.readInt());
iter = JsonIterator.parse(new ByteArrayInputStream("\"h\\\"ello\"1".getBytes()), 3);
iter.skip();
assertEquals(1, iter.readInt());
iter = JsonIterator.parse(new ByteArrayInputStream("\"\\\\\"1".getBytes()), 3);
iter.skip();
assertEquals(1, iter.readInt());
}
示例4
@Override
public int toInt() {
JsonIterator iter = parse();
try {
CodegenAccess.nextToken(iter);
return iter.readInt();
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
示例5
@Override
public long toLong() {
JsonIterator iter = parse();
try {
CodegenAccess.nextToken(iter);
return iter.readLong();
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
示例6
@Override
public float toFloat() {
JsonIterator iter = parse();
try {
CodegenAccess.nextToken(iter);
return iter.readFloat();
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
示例7
@Override
public double toDouble() {
JsonIterator iter = parse();
try {
CodegenAccess.nextToken(iter);
return iter.readDouble();
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
示例8
private void fillCache() {
if (cache == null) {
JsonIterator iter = parse();
try {
cache = iter.readString();
} catch (IOException e) {
throw new JsonException();
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
}
示例9
@Override
public boolean toBoolean() {
JsonIterator iter = parse();
try {
return CodegenAccess.readArrayStart(iter);
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
示例10
private void fillCache() {
if (lastParsedPos == tail) {
return;
}
if (cache == null) {
cache = new ArrayList<Any>(4);
}
JsonIterator iter = JsonIteratorPool.borrowJsonIterator();
try {
iter.reset(data, lastParsedPos, tail);
if (lastParsedPos == head) {
if (!CodegenAccess.readArrayStart(iter)) {
lastParsedPos = tail;
return;
}
cache.add(iter.readAny());
}
while (CodegenAccess.nextToken(iter) == ',') {
cache.add(iter.readAny());
}
lastParsedPos = tail;
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
示例11
private void fillCache() {
if (!isCached) {
JsonIterator iter = parse();
try {
cache = iter.readDouble();
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
isCached = true;
}
}
示例12
@Override
public boolean toBoolean() {
try {
JsonIterator iter = parse();
try {
return CodegenAccess.readObjectStart(iter);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
} catch (IOException e) {
throw new JsonException(e);
}
}
示例13
@Override
public boolean next() {
if (lastParsedPos == tail) {
return false;
}
if (mapIter != null) {
if (mapIter.hasNext()) {
Map.Entry<String, Any> entry = mapIter.next();
key = entry.getKey();
value = entry.getValue();
return true;
} else {
mapIter = null;
}
}
JsonIterator iter = JsonIteratorPool.borrowJsonIterator();
try {
iter.reset(data, lastParsedPos, tail);
key = CodegenAccess.readObjectFieldAsString(iter);
value = iter.readAny();
cache.put(key, value);
if (CodegenAccess.nextToken(iter) == ',') {
lastParsedPos = CodegenAccess.head(iter);
} else {
lastParsedPos = tail;
}
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
return true;
}
示例14
public final <T> T bindTo(T obj) {
JsonIterator iter = parse();
try {
return iter.read(obj);
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
示例15
public final <T> T bindTo(TypeLiteral<T> typeLiteral, T obj) {
JsonIterator iter = parse();
try {
return iter.read(typeLiteral, obj);
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
示例16
public final <T> T as(Class<T> clazz) {
JsonIterator iter = parse();
try {
return iter.read(clazz);
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
示例17
public final <T> T as(TypeLiteral<T> typeLiteral) {
JsonIterator iter = parse();
try {
return iter.read(typeLiteral);
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
示例18
private void fillCache() {
if (!isCached) {
JsonIterator iter = parse();
try {
cache = iter.readLong();
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
isCached = true;
}
}
示例19
static final double readDouble(final JsonIterator iter) throws IOException {
int oldHead = iter.head;
try {
try {
long value = IterImplNumber.readLong(iter); // without the dot & sign
if (iter.head == iter.tail) {
return value;
}
byte c = iter.buf[iter.head];
if (c == '.') {
iter.head++;
int start = iter.head;
c = iter.buf[iter.head++];
long decimalPart = readLong(iter, c);
if (decimalPart == Long.MIN_VALUE) {
return IterImplForStreaming.readDoubleSlowPath(iter);
}
decimalPart = -decimalPart;
int decimalPlaces = iter.head - start;
if (decimalPlaces > 0 && decimalPlaces < IterImplNumber.POW10.length && (iter.head - oldHead) < 10) {
return value + (decimalPart / (double) IterImplNumber.POW10[decimalPlaces]);
} else {
iter.head = oldHead;
return IterImplForStreaming.readDoubleSlowPath(iter);
}
} else {
return value;
}
} finally {
if (iter.head < iter.tail && (iter.buf[iter.head] == 'e' || iter.buf[iter.head] == 'E')) {
iter.head = oldHead;
return IterImplForStreaming.readDoubleSlowPath(iter);
}
}
} catch (JsonException e) {
iter.head = oldHead;
return IterImplForStreaming.readDoubleSlowPath(iter);
}
}
示例20
public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.out.println("StaticCodegen configClassName [outputDir]");
System.out.println("configClassName: like a.b.Config, a class defining what to codegen");
System.out.println("outputDir: if not specified, will write to source directory of configClass");
return;
}
String configClassName = args[0];
String configJavaFile = configClassName.replace('.', '/') + ".java";
String outputDir;
if (args.length > 1) {
outputDir = args[1];
} else {
if (!new File(configJavaFile).exists()) {
throw new JsonException("must execute static code generator in the java source code directory which contains: " + configJavaFile);
}
outputDir = new File(".").getAbsolutePath();
}
Class<?> clazz = Class.forName(configClassName);
StaticCodegenConfig config = (StaticCodegenConfig) clazz.newInstance();
JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
JsonStream.setMode(EncodingMode.DYNAMIC_MODE);
config.setup();
CodegenAccess.staticGenDecoders(
config.whatToCodegen(), new CodegenAccess.StaticCodegenTarget(outputDir));
com.jsoniter.output.CodegenAccess.staticGenEncoders(
config.whatToCodegen(), new com.jsoniter.output.CodegenAccess.StaticCodegenTarget(outputDir));
}
示例21
public ReflectionCollectionDecoder(Class clazz, Type[] typeArgs) {
try {
ctor = clazz.getConstructor();
} catch (NoSuchMethodException e) {
throw new JsonException(e);
}
compTypeDecoder = Codegen.getDecoder(TypeLiteral.create(typeArgs[0]).getDecoderCacheKey(), typeArgs[0]);
}
示例22
public void test_read_byte() throws IOException {
JsonIterator iter = JsonIterator.parse(new ByteArrayInputStream("1".getBytes()), 4096);
assertEquals('1', IterImpl.readByte(iter));
try {
IterImpl.readByte(iter);
fail();
} catch (JsonException e) {
}
}
示例23
public void test_read_bytes() throws IOException {
JsonIterator iter = JsonIterator.parse(new ByteArrayInputStream("12".getBytes()), 4096);
assertEquals('1', IterImpl.readByte(iter));
assertEquals('2', IterImpl.readByte(iter));
try {
IterImpl.readByte(iter);
fail();
} catch (JsonException e) {
}
}
示例24
public void skip_missing_ctor_arg() throws IOException {
JsonIterator iter = JsonIterator.parse("{}");
try {
iter.read(TestObject8.class);
fail();
} catch (JsonException e) {
System.out.println(e);
}
}
示例25
public void test_unknown_properties() throws IOException {
JsonIterator iter = JsonIterator.parse("{\"field-1\": 100, \"field-1\": 101}");
try {
iter.read(TestObject13.class);
fail();
} catch (JsonException e) {
System.out.println(e);
}
}
示例26
public void test_unknown_properties_blacklist() throws IOException {
JsonIterator iter = JsonIterator.parse("{\"field1\": 100}");
try {
iter.read(TestObject15.class);
fail();
} catch (JsonException e) {
System.out.println(e);
}
}
示例27
public void test_private_class() {
EncodingMode encodingMode = JsoniterSpi.getCurrentConfig().encodingMode();
if (EncodingMode.REFLECTION_MODE.equals(encodingMode)) {
return;
}
try {
JsonStream.serialize(new TestObject13());
fail("should throw JsonException");
} catch (JsonException ignore) {
}
}
示例28
public void test_incomplete_field_name() throws IOException {
try {
JsonIterator.parse("{\"abc").read(InheritedObject.class);
fail();
} catch (JsonException e) {
}
}
示例29
public void test_enum() throws IOException {
// JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
TestObject5 obj = JsonIterator.deserialize("{\"field1\":\"HELLO\"}", TestObject5.class);
assertEquals(TestObject5.MyEnum.HELLO, obj.field1);
try {
JsonIterator.deserialize("{\"field1\":\"HELLO1\"}", TestObject5.class);
fail();
} catch (JsonException e) {
}
obj = JsonIterator.deserialize("{\"field1\":null}", TestObject5.class);
assertNull(obj.field1);
obj = JsonIterator.deserialize("{\"field1\":\"WOW\"}", TestObject5.class);
assertEquals(TestObject5.MyEnum.WOW, obj.field1);
}
示例30
public void test_incomplete_escape() throws IOException {
JsonIterator iter = JsonIterator.parse("\"\\");
try {
iter.readString();
fail();
} catch (JsonException e) {
}
}