|
Java example source code file (MixedStreamTest.java)
The MixedStreamTest.java Java example source code/* * Copyright (C) 2010 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.gson; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.lang.reflect.Type; import java.util.Arrays; import java.util.List; import junit.framework.TestCase; public final class MixedStreamTest extends TestCase { private static final Car BLUE_MUSTANG = new Car("mustang", 0x0000FF); private static final Car BLACK_BMW = new Car("bmw", 0x000000); private static final Car RED_MIATA = new Car("miata", 0xFF0000); private static final String CARS_JSON = "[\n" + " {\n" + " \"name\": \"mustang\",\n" + " \"color\": 255\n" + " },\n" + " {\n" + " \"name\": \"bmw\",\n" + " \"color\": 0\n" + " },\n" + " {\n" + " \"name\": \"miata\",\n" + " \"color\": 16711680\n" + " }\n" + "]"; public void testWriteMixedStreamed() throws IOException { Gson gson = new Gson(); StringWriter stringWriter = new StringWriter(); JsonWriter jsonWriter = new JsonWriter(stringWriter); jsonWriter.beginArray(); jsonWriter.setIndent(" "); gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter); gson.toJson(BLACK_BMW, Car.class, jsonWriter); gson.toJson(RED_MIATA, Car.class, jsonWriter); jsonWriter.endArray(); assertEquals(CARS_JSON, stringWriter.toString()); } public void testReadMixedStreamed() throws IOException { Gson gson = new Gson(); StringReader stringReader = new StringReader(CARS_JSON); JsonReader jsonReader = new JsonReader(stringReader); jsonReader.beginArray(); assertEquals(BLUE_MUSTANG, gson.fromJson(jsonReader, Car.class)); assertEquals(BLACK_BMW, gson.fromJson(jsonReader, Car.class)); assertEquals(RED_MIATA, gson.fromJson(jsonReader, Car.class)); jsonReader.endArray(); } public void testReaderDoesNotMutateState() throws IOException { Gson gson = new Gson(); JsonReader jsonReader = new JsonReader(new StringReader(CARS_JSON)); jsonReader.beginArray(); jsonReader.setLenient(false); gson.fromJson(jsonReader, Car.class); assertFalse(jsonReader.isLenient()); jsonReader.setLenient(true); gson.fromJson(jsonReader, Car.class); assertTrue(jsonReader.isLenient()); } public void testWriteDoesNotMutateState() throws IOException { Gson gson = new Gson(); JsonWriter jsonWriter = new JsonWriter(new StringWriter()); jsonWriter.beginArray(); jsonWriter.setHtmlSafe(true); jsonWriter.setLenient(true); gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter); assertTrue(jsonWriter.isHtmlSafe()); assertTrue(jsonWriter.isLenient()); jsonWriter.setHtmlSafe(false); jsonWriter.setLenient(false); gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter); assertFalse(jsonWriter.isHtmlSafe()); assertFalse(jsonWriter.isLenient()); } public void testReadInvalidState() throws IOException { Gson gson = new Gson(); JsonReader jsonReader = new JsonReader(new StringReader(CARS_JSON)); jsonReader.beginArray(); jsonReader.beginObject(); try { gson.fromJson(jsonReader, String.class); fail(); } catch (JsonParseException expected) { } } public void testReadClosed() throws IOException { Gson gson = new Gson(); JsonReader jsonReader = new JsonReader(new StringReader(CARS_JSON)); jsonReader.close(); try { gson.fromJson(jsonReader, new TypeToken<List Other Java examples (source code examples)Here is a short list of links related to this Java MixedStreamTest.java source code file: |
... this post is sponsored by my books ... | |
![]() #1 New Release! |
![]() FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.