Package com.alibaba.fastjson

Examples of com.alibaba.fastjson.JSONReader


public class Issue72 extends TestCase {

    public void test_for_issue() throws Exception {
        InputStream is = Issue72.class.getClassLoader().getResourceAsStream("issue72.json");
        JSONReader reader = null;
        try {
            byte[] rowBatchBytes = null;
            byte[] fileBatchBytes = null;
            reader = new JSONReader(new InputStreamReader(is));
            reader.startArray();
            while (reader.hasNext()) {
                if (rowBatchBytes == null) {
                    rowBatchBytes = reader.readObject(byte[].class);
                } else if (fileBatchBytes == null) {
                    fileBatchBytes = reader.readObject(byte[].class);
                } else {
                    throw new Exception("archive data json parse failed!");
                }

            }
            reader.endArray();
        } finally {
            IOUtils.close(reader);
        }
    }
View Full Code Here


        reader.close();
    }
   
    public void test_obj_3() throws Exception {
        JSONReader reader = new JSONReader(new StringReader("{\"val\":{\"val\":{\"id\":123}}}"));

        reader.startObject();
        Assert.assertEquals("val", reader.readString());
       
        reader.startObject();
        Assert.assertEquals("val", reader.readString());

        reader.startObject();
        Assert.assertEquals("id", reader.readString());
        Assert.assertEquals(Long.valueOf(123), reader.readLong());
        reader.endObject();

        reader.endObject();
        reader.endObject();

        reader.close();
    }
View Full Code Here

        VO vo = new VO();
        vo.setValue("ABC".getBytes("UTF-8"));
       
        String text = JSON.toJSONString(vo);
       
        JSONReader reader = new JSONReader(new StringReader(text));
        VO vo2 = reader.readObject(VO.class);
        Assert.assertEquals("ABC", new String(vo2.getValue()));
        reader.close();
    }
View Full Code Here

import com.alibaba.fastjson.JSONReader;

public class JSONReader_map extends TestCase {

    public void test_array() throws Exception {
        JSONReader reader = new JSONReader(new StringReader("[{\"id\":123}]"));

        reader.startArray();

        Map<String, Object> map = new HashMap<String, Object>();
        reader.readObject(map);

        Assert.assertEquals(123, map.get("id"));

        reader.endArray();

        reader.close();
    }
View Full Code Here

        reader.close();
    }

    public void test_map() throws Exception {
        JSONReader reader = new JSONReader(new StringReader("{\"id\":123}"));

        Map<String, Object> map = new HashMap<String, Object>();
        reader.readObject(map);

        Assert.assertEquals(123, map.get("id"));

        reader.close();
    }
View Full Code Here

import com.alibaba.fastjson.JSONReader;
import com.alibaba.fastjson.parser.Feature;

public class JSONReaderTest_0 extends TestCase {
  public void test_read() throws Exception {
    JSONReader reader = new JSONReader(new StringReader("{}"));
    reader.config(Feature.AllowArbitraryCommas, true);
   
    JSONObject object = (JSONObject) reader.readObject();
    Assert.assertNotNull(object);
   
    reader.close();
  }
View Full Code Here

    public void test_read() throws Exception {
        Field field = JSONReader.class.getDeclaredField("context");
        field.setAccessible(true);
        ;

        JSONReader reader = new JSONReader(new StringReader("[{}]"));
        reader.config(Feature.AllowArbitraryCommas, true);

        reader.startArray();

        Object context = field.get(reader);
        Field stateField = context.getClass().getDeclaredField("state");
        stateField.setAccessible(true);
        stateField.set(context, -1);

        {
            Exception error = null;
            try {
                reader.startObject();
            } catch (Exception ex) {
                error = ex;
            }
            Assert.assertNotNull(error);
        }
        {
            Exception error = null;
            try {
                reader.readInteger();
            } catch (Exception ex) {
                error = ex;
            }
            Assert.assertNotNull(error);
        }
        reader.close();
    }
View Full Code Here

import com.alibaba.fastjson.JSONReader;

public class JSONReader_obj_2 extends TestCase {

    public void test_array() throws Exception {
        JSONReader reader = new JSONReader(new StringReader("[{\"id\":123}]"));

        reader.startArray();

        VO vo = reader.readObject(VO.class);

        Assert.assertEquals(123, vo.getId());

        reader.endArray();

        reader.close();
    }
View Full Code Here

        reader.close();
    }

    public void test_obj() throws Exception {
        JSONReader reader = new JSONReader(new StringReader("{\"id\":123}"));

        VO vo = reader.readObject(VO.class);

        Assert.assertEquals(123, vo.getId());

        reader.close();
    }
View Full Code Here

import com.alibaba.fastjson.JSONReader;

public class JSONReader_error extends TestCase {

    public void test_0() throws Exception {
        JSONReader reader = new JSONReader(new StringReader("[]"));

        Exception error = null;
        try {
            reader.hasNext();
        } catch (Exception e) {
            error = e;
        }
        Assert.assertNotNull(error);
       
        reader.close();
    }
View Full Code Here

TOP

Related Classes of com.alibaba.fastjson.JSONReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.