Package com.github.jsonj

Examples of com.github.jsonj.JsonObject


public class JsonBuilder {
    private final JsonObject object;

    private JsonBuilder() {
        // use the static methods
        object = new JsonObject();
    }
View Full Code Here


     * @param fields one or more Entry instances (use the field method to create them).
     * @return the JsonObject with the entries added.
     */
    @SafeVarargs
    public static JsonObject object(Entry<String,JsonElement>...fields) {
        JsonObject object = new JsonObject();
        object.add(fields);
        return object;
    }
View Full Code Here

    @SuppressWarnings("rawtypes")
    public static JsonElement fromObject(Object o) {
        if(o instanceof JsonBuilder) {
            return ((JsonBuilder) o).get();
        } else if(o instanceof Map) {
            return new JsonObject((Map)o);
        } else if(o instanceof List) {
            return new JsonArray((List)o);
        }
        return primitive(o);
    }
View Full Code Here

        return true;
    }

    public boolean startObject() {
        isObject = true;
        stack.add(new JsonObject());
        return true;
    }
View Full Code Here

        JsonElement e = stack.peekLast();
        if (e.isPrimitive()) {
            e = stack.pollLast();
            JsonElement last = stack.peekLast();
            if (last.isObject()) {
                JsonObject container = last.asObject();
                String key = e.asPrimitive().asString();
                container.put(key, value);
            } else if (last.isArray()) {
                throw new IllegalStateException("shouldn't happen");

            }
        }
View Full Code Here

        // check with the other parser as well
    }

    @Test
    public void shouldParseConcurrently() throws InterruptedException, ExecutionException, TimeoutException {
        JsonObject json = object().put("b","c").put("a", array("1","2")).put("c", object().get()).get();
        final String input = JsonSerializer.serialize(json, false);

        ExecutorService tp = Executors.newFixedThreadPool(100);

        Queue<Callable<Boolean>> tasks = new ConcurrentLinkedQueue<Callable<Boolean>>();
View Full Code Here

@Test
public class JsonSerializerTest {
    private final JsonParser jsonParser = new JsonParser();

    public void shouldDoSerializeParseRoundTrip() {
        JsonObject original = object().put("a", object().get()).put("b", "test").putArray("c", "1","2","3").get();
        String json = JsonSerializer.serialize(original, true);
        String json2 = JsonSerializer.serialize(original, false);
        // there should be a difference (pretty printing)
        AssertJUnit.assertNotSame(json, json2);
        // if we parse it back and reprint it they should be identical to each other and the original
View Full Code Here

        JsonElement parsed = new JsonParser().parse(string);
        AssertJUnit.assertEquals(e, parsed);
    }

    public void shouldUseOutputStream() throws IOException {
        JsonObject object = object(field("hi", "wrld"));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        object.serialize(bos);
        byte[] string = bos.toByteArray();
        assertThat(string.length, Matchers.greaterThan(0));
        assertThat(jsonParser.parse(bos.toString()), is(object));
    }
View Full Code Here

import com.github.jsonj.JsonObject;

@Test
public class JsonXmlConverterTest {
    public void shouldConvertToXml() {
        JsonObject object = sampleJson();
        Element element = JsonXmlConverter.getElement(object, "r");
        Document document = new Document(element);
        String xml = document.toXML();
        Assert.assertTrue(xml.contains("<r>"), "should contain the opening tag <r>");
    }
View Full Code Here

        String xml = document.toXML();
        Assert.assertTrue(xml.contains("<r>"), "should contain the opening tag <r>");
    }

    public void shouldGetW3cDomTree() {
        JsonObject sampleJson = sampleJson();
        org.w3c.dom.Document domNode = JsonXmlConverter.getW3cDocument(sampleJson, "sample");
        Assert.assertNotNull(domNode);
        Assert.assertEquals(domNode.getElementsByTagName("sample").getLength(), 1);
    }
View Full Code Here

TOP

Related Classes of com.github.jsonj.JsonObject

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.