mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Merge pull request #43 from square/jwilson_0525_okio_substring
Save an allocation by using Okio instead of substring.
This commit is contained in:
@@ -19,8 +19,8 @@ import java.io.IOException;
|
|||||||
import java.lang.reflect.AnnotatedElement;
|
import java.lang.reflect.AnnotatedElement;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import okio.Buffer;
|
import okio.Buffer;
|
||||||
|
import okio.BufferedSink;
|
||||||
import okio.BufferedSource;
|
import okio.BufferedSource;
|
||||||
import okio.Sink;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts Java values to JSON, and JSON values to Java.
|
* Converts Java values to JSON, and JSON values to Java.
|
||||||
@@ -38,10 +38,9 @@ public abstract class JsonAdapter<T> {
|
|||||||
|
|
||||||
public abstract void toJson(JsonWriter writer, T value) throws IOException;
|
public abstract void toJson(JsonWriter writer, T value) throws IOException;
|
||||||
|
|
||||||
public final void toJson(Sink sink, T value) throws IOException {
|
public final void toJson(BufferedSink sink, T value) throws IOException {
|
||||||
JsonWriter writer = new JsonWriter(sink);
|
JsonWriter writer = new JsonWriter(sink);
|
||||||
toJson(writer, value);
|
toJson(writer, value);
|
||||||
writer.flush();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public final String toJson(T value) throws IOException {
|
public final String toJson(T value) throws IOException {
|
||||||
|
@@ -19,7 +19,6 @@ import java.io.Closeable;
|
|||||||
import java.io.Flushable;
|
import java.io.Flushable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import okio.BufferedSink;
|
import okio.BufferedSink;
|
||||||
import okio.Okio;
|
|
||||||
import okio.Sink;
|
import okio.Sink;
|
||||||
|
|
||||||
import static com.squareup.moshi.JsonScope.DANGLING_NAME;
|
import static com.squareup.moshi.JsonScope.DANGLING_NAME;
|
||||||
@@ -181,11 +180,11 @@ public final class JsonWriter implements Closeable, Flushable {
|
|||||||
/**
|
/**
|
||||||
* Creates a new instance that writes a JSON-encoded stream to {@code sink}.
|
* Creates a new instance that writes a JSON-encoded stream to {@code sink}.
|
||||||
*/
|
*/
|
||||||
public JsonWriter(Sink sink) {
|
public JsonWriter(BufferedSink sink) {
|
||||||
if (sink == null) {
|
if (sink == null) {
|
||||||
throw new NullPointerException("sink == null");
|
throw new NullPointerException("sink == null");
|
||||||
}
|
}
|
||||||
this.sink = Okio.buffer(sink);
|
this.sink = sink;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -518,13 +517,13 @@ public final class JsonWriter implements Closeable, Flushable {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (last < i) {
|
if (last < i) {
|
||||||
sink.writeUtf8(value.substring(last, i));
|
sink.writeUtf8(value, last, i);
|
||||||
}
|
}
|
||||||
sink.writeUtf8(replacement);
|
sink.writeUtf8(replacement);
|
||||||
last = i + 1;
|
last = i + 1;
|
||||||
}
|
}
|
||||||
if (last < length) {
|
if (last < length) {
|
||||||
sink.writeUtf8(value.substring(last, length));
|
sink.writeUtf8(value, last, length);
|
||||||
}
|
}
|
||||||
sink.writeByte('"');
|
sink.writeByte('"');
|
||||||
}
|
}
|
||||||
|
2
pom.xml
2
pom.xml
@@ -26,7 +26,7 @@
|
|||||||
<java.version>1.7</java.version>
|
<java.version>1.7</java.version>
|
||||||
|
|
||||||
<!-- Dependencies -->
|
<!-- Dependencies -->
|
||||||
<okio.version>1.1.0</okio.version>
|
<okio.version>1.4.0</okio.version>
|
||||||
|
|
||||||
<!-- Test Dependencies -->
|
<!-- Test Dependencies -->
|
||||||
<junit.version>4.12</junit.version>
|
<junit.version>4.12</junit.version>
|
||||||
|
Reference in New Issue
Block a user