Android examples for File Input Output:Json
Print the json representation of a List to the given writer.
/*/* w w w .j a v a2 s . c o m*/ * The MIT License (MIT) * * Copyright (c) 2014 Segment, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import android.util.JsonReader; import android.util.JsonToken; import android.util.JsonWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import static com.segment.analytics.Utils.createMap; import static com.segment.analytics.Utils.isNullOrEmpty; public class Main{ /** Print the json representation of a List to the given writer. */ private static void listToWriter(List<?> list, JsonWriter writer) throws IOException { writer.beginArray(); for (Object value : list) { write(value, writer); } writer.endArray(); } private static void write(Object value, JsonWriter writer) throws IOException { if (value == null) { writer.nullValue(); } else if (value instanceof Number) { writer.value((Number) value); } else if (value instanceof Boolean) { writer.value((Boolean) value); } else if (value instanceof List) { listToWriter((List) value, writer); } else if (value instanceof Map) { mapToWriter((Map) value, writer); } else { writer.value(String.valueOf(value)); } } /** Print the json representation of a map to the given writer. */ private static void mapToWriter(Map<?, ?> map, JsonWriter writer) throws IOException { writer.beginObject(); for (Map.Entry<?, ?> entry : map.entrySet()) { Object key = entry.getKey(); Object value = entry.getValue(); writer.name(String.valueOf(key)); write(value, writer); } writer.endObject(); } }