2017-05-14
-
Moshi now uses
@Nullable
to annotate all possibly-null values. We've added a compile-time dependency on the JSR 305 annotations. This is a provided dependency and does not need to be included in your build configuration,.jar
file, or.apk
. We use@ParametersAreNonnullByDefault
and all parameters and return types are never null unless explicitly annotated@Nullable
. -
Warning: Moshi APIs in this update are source-incompatible for Kotlin callers. Nullability was previously ambiguous and lenient but now the compiler will enforce strict null checks.
-
Kotlin models are now supported via the
moshi-kotlin
extension.KotlinJsonAdapterFactory
is the best way to use Kotlin with Moshi. It honors default values and is null-safe. Kotlin users that don't use this factory should write custom adapters for their JSON types. Otherwise Moshi cannot properly initialize delegated properties of the objects it decodes. -
New: Upgrade to Okio 1.13.0.
<dependency> <groupId>com.squareup.okio</groupId> <artifactId>okio</artifactId> <version>1.13.0</version> </dependency> com.squareup.okio:okio:1.13.0
-
New: You may now declare delegates in
@ToJson
and@FromJson
methods. If one of the arguments to the method is aJsonAdapter
of the same type, that will be the next eligible adapter for that type. This may be useful for composing adapters. -
New:
Types.equals(Type, Type)
makes it easier to compare types inJsonAdapter.Factory
. -
Fix: Retain the sign on negative zero.
2017-02-04
Moshi 1.4 is a major release that adds JSON values as a core part of the library. We consider any
Java object comprised of maps, lists, strings, numbers, booleans and nulls to be a JSON value. These
are equivalent to parsed JSON objects in JavaScript, Gson’s JsonElement
, and
Jackson’s JsonNode
. Unlike Jackson and Gson, Moshi just uses Java’s built-in types for
its values:
JSON type | Java type | |
---|---|---|
{...} | Object | Map<String, Object> |
[...] | Array | List<Object> |
"abc" | String | String |
123 | Number | Double, Long, or BigDecimal |
true | Boolean | Boolean |
null | null | null |
Moshi's new API JsonAdapter.toJsonValue()
converts your application classes to JSON values
comprised of the above types. Symmetrically, JsonAdapter.fromJsonValue()
converts JSON values to
your application classes.
- New:
JsonAdapter.toJsonValue()
andfromJsonValue()
. - New:
JsonReader.readJsonValue()
reads a JSON value from a stream. - New:
Moshi.adapter(Type, Class<? extends Annotation>)
lets you look up the adapter for a qualified type. - New:
JsonAdapter.serializeNulls()
andindent()
return JSON adapters that customize the format of the encoded JSON. - New:
JsonReader.selectName()
andselectString()
optimize decoding JSON with known names and values. - New:
Types.nextAnnotations()
reduces the amount of code required to implement a customJsonAdapter.Factory
. - Fix: Don't fail on large longs that have a fractional component like
9223372036854775806.0
.
2016-10-21
- Fix: Don't incorrectly report invalid input when a slash character is escaped. When we tightened
our invalid escape handling we missed the one character that is valid both escaped
\/
and unescaped/
.
2016-10-15
-
New: Permit
@ToJson
and@FromJson
methods to take any number ofJsonAdapter
parameters to delegate to. This is supported for@ToJson
methods that take aJsonWriter
and@FromJson
methods that take aJsonReader
. -
New: Throw
JsonEncodingException
when the incoming data is not valid JSON. Use this to differentiate data format problems from connectivity problems. -
New: Upgrade to Okio 1.11.0.
<dependency> <groupId>com.squareup.okio</groupId> <artifactId>okio</artifactId> <version>1.11.0</version> </dependency>
-
New: Omit Kotlin (
kotlin.*
) and Scala (scala.*
) platform types when encoding objects using their fields. This should make it easier to avoid unexpected dependencies on platform versions. -
Fix: Explicitly limit reading and writing to 31 levels of nested structure. Previously no specific limit was enforced, but deeply nested documents would fail with either an
ArrayIndexOutOfBoundsException
due to a bug inJsonWriter
's path management, or aStackOverflowError
due to excessive recursion. -
Fix: Require enclosed types to specify their enclosing type with
Types.newParameterizedTypeWithOwner()
. Previously this API did not exist and looking up adapters for enclosed parameterized types as not possible. -
Fix: Fail on invalid escapes. Previously any character could be escaped. With this fix only characters permitted to be escaped may be escaped. Use
JsonReader.setLenient(true)
to read JSON documents that escape characters that should not be escaped.
2016-05-28
-
New: Take advantage of Okio's new
Options
feature when reading field names and enum values. This has a significant impact on performance. We measured parsing performance improve from 89k ops/sec to 140k ops/sec on one benchmark on one machine. -
New: Upgrade to Okio 1.8.0.
<dependency> <groupId>com.squareup.okio</groupId> <artifactId>okio</artifactId> <version>1.8.0</version> </dependency>
-
New: Support types that lack no-argument constructors objects on Android releases prior to Gingerbread.
-
Fix: Add writer value overload for boxed booleans. Autoboxing resolves boxed longs and doubles to
value(Number)
, but a boxed boolean would otherwise resolve to value(boolean) with an implicit call to booleanValue() which has the potential to throw NPEs. -
Fix: Be more aggressive about canonicalizing types.
2016-01-19
- New: Support RFC 7159, the latest JSON specification. This removes the constraint that the root value must be an array or an object. It may now take any value: array, object, string, number, boolean, or null. Previously this was only permitted if the adapter was configured to be lenient.
- New: Enum constants may be annotated with
@Json
to customize their encoded value. - New: Create new builder from Moshi instance with
Moshi.newBuilder()
. - New:
Types.getRawType()
andTypes.collectionElementType()
APIs to assist in defining generic type adapter factories.
2015-09-27
- API Change: Replaced
new JsonReader()
withJsonReader.of()
andnew JsonWriter()
withJsonWriter.of()
. If your code calls either of these constructors it will need to be updated to call the static factory method instead. - API Change: Don’t throw
IOException
onJsonAdapter.toJson(T)
. Code that calls this method may need to be fixed to no longer catch an impossibleIOException
. - Fix: the JSON adapter for
Object
no longer fails when encounteringnull
in the stream. - New:
@Json
annotation can customize a field's name. This is particularly handy for fields whose names are Java keywords, likedefault
orpublic
. - New:
Rfc3339DateJsonAdapter
converts between ajava.util.Date
and a string formatted with RFC 3339 (like2015-09-26T18:23:50.250Z
). This class is in the newmoshi-adapters
subproject. You will need to register this adapter if you want this date formatting behavior. See it in action in the dates example. - New:
Moshi.adapter()
keeps a cache of all created adapters. For best efficiency, application code should keep a reference to required adapters in a field. - New: The
Types
factory class makes it possible to compose types likeList<Card>
orMap<String, Integer>
. This is useful to look up JSON adapters for parameterized types. - New:
JsonAdapter.failOnUnknown()
returns a new JSON adapter that throws if an unknown value is encountered on the stream. Use this in development and debug builds to detect typos in field names. This feature shouldn’t be used in production because it makes migrations very difficult.
2015-06-16
- Databinding for primitive types, strings, enums, arrays, collections, and maps.
- Databinding for plain old Java objects.
- JSONPath support for both
JsonReader
andJsonWriter
. - Throw
JsonDataException
when there’s a data binding problem. - Adapter methods:
@ToJson
and@FromJson
. - Qualifier annotations:
@JsonQualifier
to permit different type adapters for the same Java type. - Imported code from Gson:
JsonReader
,JsonWriter
. Also some internal classes:LinkedHashTreeMap
for hash-collision avoidance andTypes
for typesafe databinding.