Get rid of jackson (mostly)
Reviewed By: astreet Differential Revision: D3041362 fb-gh-sync-id: a7027a08a63730b98dc766b86921820fa3624953 shipit-source-id: a7027a08a63730b98dc766b86921820fa3624953
This commit is contained in:
parent
14555063bb
commit
d329570ac8
|
@ -9,8 +9,6 @@
|
||||||
|
|
||||||
package com.facebook.react.bridge;
|
package com.facebook.react.bridge;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
|
||||||
|
|
||||||
import com.facebook.infer.annotation.Assertions;
|
import com.facebook.infer.annotation.Assertions;
|
||||||
import com.facebook.systrace.Systrace;
|
import com.facebook.systrace.Systrace;
|
||||||
|
|
||||||
|
@ -325,20 +323,18 @@ public abstract class BaseJavaModule implements NativeModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void writeConstantsField(JsonGenerator jg, String fieldName) throws IOException {
|
public final void writeConstantsField(JsonWriter writer, String fieldName) throws IOException {
|
||||||
Map<String, Object> constants = getConstants();
|
Map<String, Object> constants = getConstants();
|
||||||
if (constants == null || constants.isEmpty()) {
|
if (constants == null || constants.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
jg.writeObjectFieldStart(fieldName);
|
writer.name(fieldName).beginObject();
|
||||||
for (Map.Entry<String, Object> constant : constants.entrySet()) {
|
for (Map.Entry<String, Object> constant : constants.entrySet()) {
|
||||||
JsonGeneratorHelper.writeObjectField(
|
writer.name(constant.getKey());
|
||||||
jg,
|
JsonWriterHelper.value(writer, constant.getValue());
|
||||||
constant.getKey(),
|
|
||||||
constant.getValue());
|
|
||||||
}
|
}
|
||||||
jg.writeEndObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -31,9 +31,6 @@ import com.facebook.react.common.futures.SimpleSettableFuture;
|
||||||
import com.facebook.systrace.Systrace;
|
import com.facebook.systrace.Systrace;
|
||||||
import com.facebook.systrace.TraceListener;
|
import com.facebook.systrace.TraceListener;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonFactory;
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This provides an implementation of the public CatalystInstance instance. It is public because
|
* This provides an implementation of the public CatalystInstance instance. It is public because
|
||||||
* it is built by ReactInstanceManager which is in a different package.
|
* it is built by ReactInstanceManager which is in a different package.
|
||||||
|
@ -358,21 +355,24 @@ public class CatalystInstanceImpl implements CatalystInstance {
|
||||||
private String buildModulesConfigJSONProperty(
|
private String buildModulesConfigJSONProperty(
|
||||||
NativeModuleRegistry nativeModuleRegistry,
|
NativeModuleRegistry nativeModuleRegistry,
|
||||||
JavaScriptModulesConfig jsModulesConfig) {
|
JavaScriptModulesConfig jsModulesConfig) {
|
||||||
JsonFactory jsonFactory = new JsonFactory();
|
StringWriter stringWriter = new StringWriter();
|
||||||
StringWriter writer = new StringWriter();
|
JsonWriter writer = new JsonWriter(stringWriter);
|
||||||
try {
|
try {
|
||||||
JsonGenerator jg = jsonFactory.createGenerator(writer);
|
writer.beginObject();
|
||||||
jg.writeStartObject();
|
writer.name("remoteModuleConfig");
|
||||||
jg.writeFieldName("remoteModuleConfig");
|
nativeModuleRegistry.writeModuleDescriptions(writer);
|
||||||
nativeModuleRegistry.writeModuleDescriptions(jg);
|
writer.name("localModulesConfig");
|
||||||
jg.writeFieldName("localModulesConfig");
|
jsModulesConfig.writeModuleDescriptions(writer);
|
||||||
jsModulesConfig.writeModuleDescriptions(jg);
|
writer.endObject();
|
||||||
jg.writeEndObject();
|
return stringWriter.toString();
|
||||||
jg.close();
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
throw new RuntimeException("Unable to serialize JavaScript module declaration", ioe);
|
throw new RuntimeException("Unable to serialize JavaScript module declaration", ioe);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
writer.close();
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return writer.getBuffer().toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void incrementPendingJSCalls() {
|
private void incrementPendingJSCalls() {
|
||||||
|
|
|
@ -14,8 +14,6 @@ import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class stores configuration of javascript modules that can be used across the bridge
|
* Class stores configuration of javascript modules that can be used across the bridge
|
||||||
*/
|
*/
|
||||||
|
@ -31,29 +29,29 @@ public class JavaScriptModulesConfig {
|
||||||
return mModules;
|
return mModules;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeModuleDescriptions(JsonGenerator jg) throws IOException {
|
public void writeModuleDescriptions(JsonWriter writer) throws IOException {
|
||||||
jg.writeStartObject();
|
writer.beginObject();
|
||||||
for (JavaScriptModuleRegistration registration : mModules) {
|
for (JavaScriptModuleRegistration registration : mModules) {
|
||||||
jg.writeObjectFieldStart(registration.getName());
|
writer.name(registration.getName()).beginObject();
|
||||||
appendJSModuleToJSONObject(jg, registration);
|
appendJSModuleToJSONObject(writer, registration);
|
||||||
jg.writeEndObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
jg.writeEndObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void appendJSModuleToJSONObject(
|
private void appendJSModuleToJSONObject(
|
||||||
JsonGenerator jg,
|
JsonWriter writer,
|
||||||
JavaScriptModuleRegistration registration) throws IOException {
|
JavaScriptModuleRegistration registration) throws IOException {
|
||||||
jg.writeObjectField("moduleID", registration.getModuleId());
|
writer.name("moduleID").value(registration.getModuleId());
|
||||||
jg.writeObjectFieldStart("methods");
|
writer.name("methods").beginObject();
|
||||||
for (Method method : registration.getMethods()) {
|
for (Method method : registration.getMethods()) {
|
||||||
jg.writeObjectFieldStart(method.getName());
|
writer.name(method.getName()).beginObject();
|
||||||
jg.writeObjectField("methodID", registration.getMethodId(method));
|
writer.name("methodID").value(registration.getMethodId(method));
|
||||||
jg.writeEndObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
jg.writeEndObject();
|
writer.endObject();
|
||||||
if (registration.getModuleInterface().isAnnotationPresent(SupportsWebWorkers.class)) {
|
if (registration.getModuleInterface().isAnnotationPresent(SupportsWebWorkers.class)) {
|
||||||
jg.writeBooleanField("supportsWebWorkers", true);
|
writer.name("supportsWebWorkers").value(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,54 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (c) 2015-present, Facebook, Inc.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the BSD-style license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree. An additional grant
|
|
||||||
* of patent rights can be found in the PATENTS file in the same directory.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.facebook.react.bridge;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper for generating JSON for lists and maps.
|
|
||||||
*/
|
|
||||||
public class JsonGeneratorHelper {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Like {@link JsonGenerator#writeObjectField(String, Object)} but supports Maps and Lists.
|
|
||||||
*/
|
|
||||||
public static void writeObjectField(JsonGenerator jg, String name, Object object)
|
|
||||||
throws IOException {
|
|
||||||
if (object instanceof Map) {
|
|
||||||
writeMap(jg, name, (Map) object);
|
|
||||||
} else if (object instanceof List) {
|
|
||||||
writeList(jg, name, (List) object);
|
|
||||||
} else {
|
|
||||||
jg.writeObjectField(name, object);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void writeMap(JsonGenerator jg, String name, Map map) throws IOException {
|
|
||||||
jg.writeObjectFieldStart(name);
|
|
||||||
Set<Map.Entry> entries = map.entrySet();
|
|
||||||
for (Map.Entry entry : entries) {
|
|
||||||
writeObjectField(jg, entry.getKey().toString(), entry.getValue());
|
|
||||||
}
|
|
||||||
jg.writeEndObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void writeList(JsonGenerator jg, String name, List list) throws IOException {
|
|
||||||
jg.writeArrayFieldStart(name);
|
|
||||||
for (Object item : list) {
|
|
||||||
jg.writeObject(item);
|
|
||||||
}
|
|
||||||
jg.writeEndArray();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,218 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2015-present, Facebook, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the BSD-style license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.facebook.react.bridge;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.Writer;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Deque;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple Json generator that does no validation.
|
||||||
|
*/
|
||||||
|
public class JsonWriter implements Closeable {
|
||||||
|
private final Writer mWriter;
|
||||||
|
private final Deque<Scope> mScopes;
|
||||||
|
|
||||||
|
public JsonWriter(Writer writer) {
|
||||||
|
mWriter = writer;
|
||||||
|
mScopes = new ArrayDeque<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonWriter beginArray() throws IOException {
|
||||||
|
open(Scope.EMPTY_ARRAY, '[');
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonWriter endArray() throws IOException {
|
||||||
|
close(']');
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonWriter beginObject() throws IOException {
|
||||||
|
open(Scope.EMPTY_OBJECT, '{');
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonWriter endObject() throws IOException {
|
||||||
|
close('}');
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonWriter name(String name) throws IOException {
|
||||||
|
if (name == null) {
|
||||||
|
throw new NullPointerException("name can not be null");
|
||||||
|
}
|
||||||
|
beforeName();
|
||||||
|
string(name);
|
||||||
|
mWriter.write(':');
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonWriter value(String value) throws IOException {
|
||||||
|
if (value == null) {
|
||||||
|
return nullValue();
|
||||||
|
}
|
||||||
|
beforeValue();
|
||||||
|
string(value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonWriter nullValue() throws IOException {
|
||||||
|
beforeValue();
|
||||||
|
mWriter.write("null");
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonWriter rawValue(String json) throws IOException {
|
||||||
|
beforeValue();
|
||||||
|
mWriter.write(json);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonWriter value(boolean value) throws IOException {
|
||||||
|
beforeValue();
|
||||||
|
mWriter.write(value ? "true" : "false");
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonWriter value(double value) throws IOException {
|
||||||
|
beforeValue();
|
||||||
|
mWriter.append(Double.toString(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonWriter value(long value) throws IOException {
|
||||||
|
beforeValue();
|
||||||
|
mWriter.write(Long.toString(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonWriter value(Number value) throws IOException {
|
||||||
|
if (value == null) {
|
||||||
|
return nullValue();
|
||||||
|
}
|
||||||
|
beforeValue();
|
||||||
|
mWriter.append(value.toString());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
mWriter.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void beforeValue() throws IOException {
|
||||||
|
Scope scope = mScopes.peek();
|
||||||
|
switch (scope) {
|
||||||
|
case EMPTY_ARRAY:
|
||||||
|
replace(Scope.ARRAY);
|
||||||
|
break;
|
||||||
|
case EMPTY_OBJECT:
|
||||||
|
throw new IllegalArgumentException(Scope.EMPTY_OBJECT.name());
|
||||||
|
case ARRAY:
|
||||||
|
mWriter.write(',');
|
||||||
|
break;
|
||||||
|
case OBJECT:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalStateException("Unknown scope: " + scope);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void beforeName() throws IOException {
|
||||||
|
Scope scope = mScopes.peek();
|
||||||
|
switch (scope) {
|
||||||
|
case EMPTY_ARRAY:
|
||||||
|
case ARRAY:
|
||||||
|
throw new IllegalStateException("name not allowed in array");
|
||||||
|
case EMPTY_OBJECT:
|
||||||
|
replace(Scope.OBJECT);
|
||||||
|
break;
|
||||||
|
case OBJECT:
|
||||||
|
mWriter.write(',');
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalStateException("Unknown scope: " + scope);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void open(Scope scope, char bracket) throws IOException {
|
||||||
|
mScopes.push(scope);
|
||||||
|
mWriter.write(bracket);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void close(char bracket) throws IOException {
|
||||||
|
mScopes.pop();
|
||||||
|
mWriter.write(bracket);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void string(String string) throws IOException {
|
||||||
|
mWriter.write('"');
|
||||||
|
for (int i = 0, length = string.length(); i < length; i++) {
|
||||||
|
char c = string.charAt(i);
|
||||||
|
switch (c) {
|
||||||
|
case '\t':
|
||||||
|
mWriter.write("\\t");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '\b':
|
||||||
|
mWriter.write("\\b");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '\n':
|
||||||
|
mWriter.write("\\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '\r':
|
||||||
|
mWriter.write("\\r");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '\f':
|
||||||
|
mWriter.write("\\f");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '"':
|
||||||
|
case '\\':
|
||||||
|
mWriter.write('\\');
|
||||||
|
mWriter.write(c);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '\u2028':
|
||||||
|
case '\u2029':
|
||||||
|
mWriter.write(String.format("\\u%04x", (int) c));
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
if (c <= 0x1F) {
|
||||||
|
mWriter.write(String.format("\\u%04x", (int) c));
|
||||||
|
} else {
|
||||||
|
mWriter.write(c);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
mWriter.write('"');
|
||||||
|
}
|
||||||
|
|
||||||
|
private void replace(Scope scope) {
|
||||||
|
mScopes.pop();
|
||||||
|
mScopes.push(scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum Scope {
|
||||||
|
EMPTY_OBJECT,
|
||||||
|
OBJECT,
|
||||||
|
EMPTY_ARRAY,
|
||||||
|
ARRAY
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,60 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2015-present, Facebook, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the BSD-style license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,8 +12,6 @@ package com.facebook.react.bridge;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A native module whose API can be provided to JS catalyst instances. {@link NativeModule}s whose
|
* A native module whose API can be provided to JS catalyst instances. {@link NativeModule}s whose
|
||||||
* implementation is written in Java should extend {@link BaseJavaModule} or {@link
|
* implementation is written in Java should extend {@link BaseJavaModule} or {@link
|
||||||
|
@ -42,7 +40,7 @@ public interface NativeModule {
|
||||||
* Append a field which represents the constants this module exports
|
* Append a field which represents the constants this module exports
|
||||||
* to JS. If no constants are exported this should do nothing.
|
* to JS. If no constants are exported this should do nothing.
|
||||||
*/
|
*/
|
||||||
void writeConstantsField(JsonGenerator jg, String fieldName) throws IOException;
|
void writeConstantsField(JsonWriter writer, String fieldName) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is called at the end of {@link CatalystApplicationFragment#createCatalystInstance()}
|
* This is called at the end of {@link CatalystApplicationFragment#createCatalystInstance()}
|
||||||
|
|
|
@ -20,8 +20,6 @@ import com.facebook.react.common.MapBuilder;
|
||||||
import com.facebook.infer.annotation.Assertions;
|
import com.facebook.infer.annotation.Assertions;
|
||||||
import com.facebook.systrace.Systrace;
|
import com.facebook.systrace.Systrace;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A set of Java APIs to expose to a particular JavaScript instance.
|
* A set of Java APIs to expose to a particular JavaScript instance.
|
||||||
*/
|
*/
|
||||||
|
@ -64,27 +62,27 @@ public class NativeModuleRegistry {
|
||||||
definition.call(catalystInstance, executorToken, methodId, parameters);
|
definition.call(catalystInstance, executorToken, methodId, parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* package */ void writeModuleDescriptions(JsonGenerator jg) throws IOException {
|
/* package */ void writeModuleDescriptions(JsonWriter writer) throws IOException {
|
||||||
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "CreateJSON");
|
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "CreateJSON");
|
||||||
try {
|
try {
|
||||||
jg.writeStartObject();
|
writer.beginObject();
|
||||||
for (ModuleDefinition moduleDef : mModuleTable) {
|
for (ModuleDefinition moduleDef : mModuleTable) {
|
||||||
jg.writeObjectFieldStart(moduleDef.name);
|
writer.name(moduleDef.name).beginObject();
|
||||||
jg.writeNumberField("moduleID", moduleDef.id);
|
writer.name("moduleID").value(moduleDef.id);
|
||||||
jg.writeBooleanField("supportsWebWorkers", moduleDef.target.supportsWebWorkers());
|
writer.name("supportsWebWorkers").value(moduleDef.target.supportsWebWorkers());
|
||||||
jg.writeObjectFieldStart("methods");
|
writer.name("methods").beginObject();
|
||||||
for (int i = 0; i < moduleDef.methods.size(); i++) {
|
for (int i = 0; i < moduleDef.methods.size(); i++) {
|
||||||
MethodRegistration method = moduleDef.methods.get(i);
|
MethodRegistration method = moduleDef.methods.get(i);
|
||||||
jg.writeObjectFieldStart(method.name);
|
writer.name(method.name).beginObject();
|
||||||
jg.writeNumberField("methodID", i);
|
writer.name("methodID").value(i);
|
||||||
jg.writeStringField("type", method.method.getType());
|
writer.name("type").value(method.method.getType());
|
||||||
jg.writeEndObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
jg.writeEndObject();
|
writer.endObject();
|
||||||
moduleDef.target.writeConstantsField(jg, "constants");
|
moduleDef.target.writeConstantsField(writer, "constants");
|
||||||
jg.writeEndObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
jg.writeEndObject();
|
writer.endObject();
|
||||||
} finally {
|
} finally {
|
||||||
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
|
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,12 +8,12 @@ android_library(
|
||||||
name = 'testhelpers',
|
name = 'testhelpers',
|
||||||
srcs = glob(['*.java'], excludes = STANDARD_TEST_SRCS),
|
srcs = glob(['*.java'], excludes = STANDARD_TEST_SRCS),
|
||||||
deps = [
|
deps = [
|
||||||
react_native_target('java/com/facebook/react/bridge:bridge'),
|
|
||||||
react_native_target('java/com/facebook/react/uimanager:uimanager'),
|
|
||||||
react_native_dep('third-party/java/mockito:mockito'),
|
react_native_dep('third-party/java/mockito:mockito'),
|
||||||
react_native_dep('third-party/java/robolectric3/robolectric:robolectric'),
|
react_native_dep('third-party/java/robolectric3/robolectric:robolectric'),
|
||||||
|
react_native_target('java/com/facebook/react/bridge:bridge'),
|
||||||
|
react_native_target('java/com/facebook/react/uimanager:uimanager'),
|
||||||
react_native_tests_target('java/org/mockito/configuration:configuration'),
|
react_native_tests_target('java/org/mockito/configuration:configuration'),
|
||||||
],
|
],
|
||||||
visibility = [
|
visibility = [
|
||||||
'PUBLIC'
|
'PUBLIC'
|
||||||
],
|
],
|
||||||
|
@ -26,19 +26,18 @@ robolectric3_test(
|
||||||
srcs = glob(STANDARD_TEST_SRCS),
|
srcs = glob(STANDARD_TEST_SRCS),
|
||||||
deps = [
|
deps = [
|
||||||
':testhelpers',
|
':testhelpers',
|
||||||
react_native_target('java/com/facebook/react/bridge:bridge'),
|
react_native_dep('libraries/fbcore/src/test/java/com/facebook/powermock:powermock'),
|
||||||
react_native_target('java/com/facebook/react/common:common'),
|
react_native_dep('libraries/soloader/java/com/facebook/soloader:soloader'),
|
||||||
react_native_target('java/com/facebook/react/uimanager:uimanager'),
|
|
||||||
|
|
||||||
react_native_dep('third-party/java/robolectric3/robolectric:robolectric'),
|
|
||||||
react_native_dep('third-party/java/fest:fest'),
|
react_native_dep('third-party/java/fest:fest'),
|
||||||
react_native_dep('third-party/java/junit:junit'),
|
|
||||||
react_native_dep('third-party/java/mockito:mockito'),
|
|
||||||
react_native_dep('third-party/java/jackson:core'),
|
react_native_dep('third-party/java/jackson:core'),
|
||||||
react_native_dep('third-party/java/jackson:jackson'),
|
react_native_dep('third-party/java/jackson:jackson'),
|
||||||
react_native_dep('third-party/java/jsr-305:jsr-305'),
|
react_native_dep('third-party/java/jsr-305:jsr-305'),
|
||||||
react_native_dep('libraries/fbcore/src/test/java/com/facebook/powermock:powermock'),
|
react_native_dep('third-party/java/junit:junit'),
|
||||||
react_native_dep('libraries/soloader/java/com/facebook/soloader:soloader'),
|
react_native_dep('third-party/java/mockito:mockito'),
|
||||||
|
react_native_dep('third-party/java/robolectric3/robolectric:robolectric'),
|
||||||
|
react_native_target('java/com/facebook/react/bridge:bridge'),
|
||||||
|
react_native_target('java/com/facebook/react/common:common'),
|
||||||
|
react_native_target('java/com/facebook/react/uimanager:uimanager'),
|
||||||
],
|
],
|
||||||
visibility = [
|
visibility = [
|
||||||
'PUBLIC'
|
'PUBLIC'
|
||||||
|
|
|
@ -12,8 +12,6 @@ package com.facebook.react.bridge;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonFactory;
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -82,12 +80,11 @@ public class JavaScriptModuleConfigTest {
|
||||||
|
|
||||||
private static String getModuleDescriptions(JavaScriptModulesConfig jsModulesConfig)
|
private static String getModuleDescriptions(JavaScriptModulesConfig jsModulesConfig)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
JsonFactory jsonFactory = new JsonFactory();
|
StringWriter stringWriter = new StringWriter();
|
||||||
StringWriter writer = new StringWriter();
|
JsonWriter writer = new JsonWriter(stringWriter);
|
||||||
JsonGenerator jg = jsonFactory.createGenerator(writer);
|
jsModulesConfig.writeModuleDescriptions(writer);
|
||||||
jsModulesConfig.writeModuleDescriptions(jg);
|
writer.close();
|
||||||
jg.close();
|
return stringWriter.getBuffer().toString();
|
||||||
return writer.getBuffer().toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonNode parse(String json) throws Exception {
|
private JsonNode parse(String json) throws Exception {
|
||||||
|
|
|
@ -0,0 +1,100 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2015-present, Facebook, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the BSD-style license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.facebook.react.bridge;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.fest.assertions.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class JsonWriterTest {
|
||||||
|
private final StringWriter mStringWriter;
|
||||||
|
private final JsonWriter mWriter;
|
||||||
|
|
||||||
|
public JsonWriterTest() {
|
||||||
|
mStringWriter = new StringWriter();
|
||||||
|
mWriter = new JsonWriter(mStringWriter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void emptyObject() throws IOException {
|
||||||
|
mWriter.beginObject();
|
||||||
|
mWriter.endObject();
|
||||||
|
verify("{}");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void emptyNestedObject() throws IOException {
|
||||||
|
mWriter.beginObject();
|
||||||
|
mWriter.beginObject();
|
||||||
|
mWriter.endObject();
|
||||||
|
mWriter.endObject();
|
||||||
|
verify("{{}}");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void emptyArray() throws IOException {
|
||||||
|
mWriter.beginArray();
|
||||||
|
mWriter.endArray();
|
||||||
|
verify("[]");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void emptyNestedArray() throws IOException {
|
||||||
|
mWriter.beginArray();
|
||||||
|
mWriter.beginArray();
|
||||||
|
mWriter.endArray();
|
||||||
|
mWriter.endArray();
|
||||||
|
verify("[[]]");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void smallObject() throws IOException {
|
||||||
|
mWriter.beginObject();
|
||||||
|
mWriter.name("hello").value(true);
|
||||||
|
mWriter.name("hello_again").value("hi!");
|
||||||
|
mWriter.endObject();
|
||||||
|
verify("{\"hello\":true,\"hello_again\":\"hi!\"}");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void smallArray() throws IOException {
|
||||||
|
mWriter.beginArray();
|
||||||
|
mWriter.value(true);
|
||||||
|
mWriter.value(1);
|
||||||
|
mWriter.value(1.0);
|
||||||
|
mWriter.value("hi!");
|
||||||
|
mWriter.endArray();
|
||||||
|
verify("[true,1,1.0,\"hi!\"]");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void string() throws IOException {
|
||||||
|
mWriter.beginObject();
|
||||||
|
mWriter.name("string").value("hello!");
|
||||||
|
mWriter.endObject();
|
||||||
|
verify("{\"string\":\"hello!\"}");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void complexString() throws IOException {
|
||||||
|
mWriter.beginObject();
|
||||||
|
mWriter.name("string").value("\t\uD83D\uDCA9");
|
||||||
|
mWriter.endObject();
|
||||||
|
verify("{\"string\":\"\\t\uD83D\uDCA9\"}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void verify(String expected) throws IOException {
|
||||||
|
mWriter.close();
|
||||||
|
assertThat(mStringWriter.getBuffer().toString()).isEqualTo(expected);
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,8 +20,6 @@ import java.util.Map;
|
||||||
|
|
||||||
import com.facebook.react.common.MapBuilder;
|
import com.facebook.react.common.MapBuilder;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonFactory;
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.node.TextNode;
|
import com.fasterxml.jackson.databind.node.TextNode;
|
||||||
|
@ -189,12 +187,11 @@ public class NativeModuleRegistryTest {
|
||||||
|
|
||||||
private static String getModuleDescriptions(NativeModuleRegistry registry)
|
private static String getModuleDescriptions(NativeModuleRegistry registry)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
JsonFactory jsonFactory = new JsonFactory();
|
StringWriter stringWriter = new StringWriter();
|
||||||
StringWriter writer = new StringWriter();
|
JsonWriter writer = new JsonWriter(stringWriter);
|
||||||
JsonGenerator jg = jsonFactory.createGenerator(writer);
|
registry.writeModuleDescriptions(writer);
|
||||||
registry.writeModuleDescriptions(jg);
|
writer.close();
|
||||||
jg.close();
|
return stringWriter.getBuffer().toString();
|
||||||
return writer.getBuffer().toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class MethodsModule extends BaseJavaModule {
|
private static class MethodsModule extends BaseJavaModule {
|
||||||
|
|
Loading…
Reference in New Issue