mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
1151c096da
Summary: This change drops the year from the copyright headers and the LICENSE file. Reviewed By: yungsters Differential Revision: D9727774 fbshipit-source-id: df4fc1e4390733fe774b1a160dd41b4a3d83302a
59 lines
1.6 KiB
Java
59 lines
1.6 KiB
Java
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
package com.facebook.react.bridge;
|
|
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* Helper for generating JSON for lists and maps.
|
|
*/
|
|
class JsonWriterHelper {
|
|
public static void value(JsonWriter writer, Object value) throws IOException {
|
|
if (value instanceof Map) {
|
|
mapValue(writer, (Map) value);
|
|
} else if (value instanceof List) {
|
|
listValue(writer, (List) value);
|
|
} else {
|
|
objectValue(writer, value);
|
|
}
|
|
}
|
|
|
|
private static void mapValue(JsonWriter writer, Map<?, ?> map) throws IOException {
|
|
writer.beginObject();
|
|
for (Map.Entry entry : map.entrySet()) {
|
|
writer.name(entry.getKey().toString());
|
|
value(writer, entry.getValue());
|
|
}
|
|
writer.endObject();
|
|
}
|
|
|
|
private static void listValue(JsonWriter writer, List<?> list) throws IOException {
|
|
writer.beginArray();
|
|
for (Object item : list) {
|
|
objectValue(writer, item);
|
|
}
|
|
writer.endArray();
|
|
}
|
|
|
|
private static void objectValue(JsonWriter writer, Object value) throws IOException {
|
|
if (value == null) {
|
|
writer.nullValue();
|
|
} else if (value instanceof String) {
|
|
writer.value((String) value);
|
|
} else if (value instanceof Number) {
|
|
writer.value((Number) value);
|
|
} else if (value instanceof Boolean) {
|
|
writer.value((Boolean) value);
|
|
} else {
|
|
throw new IllegalArgumentException("Unknown value: " + value);
|
|
}
|
|
}
|
|
}
|