Commit Graph

311 Commits

Author SHA1 Message Date
Jesse Wilson
b96397f6eb Merge pull request #500 from square/eric.coherent-nesting-problem-error-message
Add coherent error message for uneconded map keys.
2018-04-28 21:14:45 -04:00
Eric Cochran
44e6fbd067 Add coherent error message for unencoded map keys. 2018-04-25 11:36:47 -07:00
Eric Cochran
b125f06e70 Fix up out-of-date comments. 2018-04-24 13:17:40 -07:00
Eric Cochran
84745b0537 Disallow null Type from entering user code.
Otherwise, Moshi.adapter(null) will end up calling into user JsonAdapter factories with a null Type parameter.
Kotlin fails with java.lang.IllegalArgumentException: Parameter specified as non-null is null
That message ends up too far away from the real error.
2018-04-15 21:04:17 -07:00
Eric Cochran
b860b6da4f Make error message for dangling names consistent. (#501) 2018-04-15 19:48:32 -07:00
Jesse Wilson
9401a810f0 Support generated adapters for Kotlin superclasses 2018-04-15 14:37:49 -04:00
Eric Cochran
78091aeb46 Fix JsonUtf8Writer to be strict about names in the wrong place. (#502) 2018-04-15 09:39:04 -04:00
Jesse Wilson
0a49ae3ac8 Merge pull request #491 from square/eric.jsonclass-data
Remove data class limitation from JsonClass doc.
2018-04-08 05:26:37 -07:00
Jesse Wilson
ceef5dc682 Merge pull request #493 from square/eric.never-type-equals
Fix type checks with custom adapters.
2018-04-08 05:26:13 -07:00
Eric Cochran
ba1318cc45 Make "no adapter" error message friendlier.
In the very common case, there are no JsonQualifier annotations for the type.
2018-04-08 00:48:38 -07:00
Eric Cochran
fa1f10dc77 Fix type checks with custom adapters.
Moshi.Builder.add(Type, ...) adds a factory that had a broken type equality check.
Closes #128
2018-04-08 00:42:46 -07:00
Eric Cochran
c4a2e7657f Remove data class limitation from JsonClass doc. 2018-04-07 20:33:22 -07:00
Jesse Wilson
466f77aabe JsonAdapter.nonNull() forbids explicit nulls in the JSON body
This adapter modifier throws exceptions if an unexpected null is
encountered. This may pair nicely with Kotlin.
2018-04-04 23:08:36 -07:00
Eric Cochran
a931184edf Hide Types.resolve. 2018-04-03 23:35:32 -07:00
Jesse Wilson
c1b93247e3 Merge pull request #475 from square/eric.resolve-generics
Resolve generic property types in KotlinJsonAdapter.
2018-04-04 01:18:31 -04:00
Eric Cochran
dc450e6192 Resolve generic property types in KotlinJsonAdapter. 2018-04-03 17:37:38 -07:00
Jesse Wilson
982f9c94f6 Fold the kotlin-codegen-runtime into Moshi itself.
Rename @MoshiSerializable to @JsonClass. Like @Json, I'm anticipating
a future where there are other interesting properties on this annotation.
Perhaps a future feature where Moshi is strict and only adapts types that
have a '@JsonClass' annotation.

Also rename MoshiKotlinCodeGenProcessor to JsonClassCodeGenProcessor. We
may later support other ways of generating code here; perhaps for regular
Java types.
2018-03-28 20:26:20 -04:00
Zac Sweers
96e074d030 Kotlin Code Gen module (#435)
* Add kotlin code gen modules

* Update kotlin to 1.2

* Add a serializable dummy class

* Try using kapt configuration from kotlin-examples repo

Still no luck!

* Use proper allocated name for assignment too

* Use selectName() API

* Clean up constructor parameter annotations & plumbing for qualifiers

* Updates poms and kotlin code gen processor to support tests.

* Ignore kotlin code gen tests for now

None of these are data classes tests right now, which is the only thing this supports right now

* Replace $ with _ in class names for consistency

* Shortcut Array types to arrayOf

* Add DataClassTest

* Try generated option first, fall back to maven after

* More idiomatic handling

* Only use nonnullable types for adapter properties

* Code dump of kotshi tests

* Comment out specifics to get compiling

* Generics support!

* Fix double primitive default

* Pick up temporary snapshot for Any fix

* Invariance should just be null

* Better handling of nullably-bound variance

* Just assume the first jvm constructor for now as jvmMethodSig is flaky

* Specify types param if needed

* Don't do lazy delegation

* Clean up nullable typevariablename boundaries

* Add type variables to extension function on companion object

* Use properties instead of allocated names for more robustness

Since we're already on a snapshot

* If there are no type variables, make it null for simpler handling

* Fix generics and Type[] handling

* Fix unnecessary as casts on primitive defaults

* Reference spec directly for possible bangs

* Use nullSafe() adapters for anything nullable or with default values

* Use object type in makeType()

Types.java cares

* Make TestPrimitiveDefaultValues work

* Re-enable TestClassWithJavaKeyword

* Ignore remaining tests that are pending decisions or JsonQualifier support

* Remove customnames test as we're just going to stick with simple @Json

* Add toString() implementations

* Reenable default values testing, adapt to kotlin lang support

* Remove primitive adapters bits since we're not using it

* Clean up a bunch of leftover comments

* Switch to only nullable handling, report missing properties

This makes all nullable handling for local properties the same, and removes defaults for primitives in the process. It simplifies the handling a lot, and leans on kotlin language features to take care of null handling (null checking and then throwing the lazily evaluated list of missing properties).

One minor change from what kotshi does - this reports the serialized name in the missing properties, not the property name. We could look at supporting this though if we want.

* Implement JsonQualifier support

* Use Kapt for AutoService/processor declaration

* Checkstyle

* Remove unused primite type checks

* Add test verifying mutable and immutable collections work

* Fix test name

* Standardize isRequired checks

* Add more nullability and mutability tests

* Kotlinpoet 0.7.0 final

* Switch to new vararg overload for annotation class adapter()

* Make suffix just JsonAdapter without underscore

* Switch to just a regular constructor for MoshiSerializableFactory

* Remove constructor caching

* Remove unnecessary framework class checks

* Nix unnecessary superclass lookups, inline constructor lookup

* Nix null token check in reads

* Nix null check in writes, do !! on first value use

* Nix null checks in favor of serializeNulls

* Inline null checks and fail eagerly

* Fix double _Adapter

* First pass at simplifying adapter names

* Inline names to options property, life into class and rm companion

* Differentiate between absent and null, use nullSafe() as needed

* Group together compile and test dependencies

* Remove incorrect comment

* Revert formatting

* Set, not mutable set

* Collapse else-if nesting to one when

* Cleaner formatting test code

* Collapse more to locals

* Collapse more

* Return a nonnullable type in fromJson

* Remove redundant out variance

* Use KClass where appropriate

* End comment in period

* Remove redundant comment

* Throw on unrecognized type in simplified name

* Use illegalargumentexception instead

* Emit a nullcheck at the beginning of toJson instead

* Remove extra newline

* Simplify processing to be less abusive

* Skip using asClassName() when possible

* Use addComment()

* Switch to declared constructors

Technically more correct since we're defining these

* Unmodifiable set

* return adapter(type, annotationTypes[0])

* Slight optimization - check if the type is parameterized first

If the type is a parameterized type, then we know they'll have the two-arg constructor. This way we don't always try and fail the single arg constructor on parameterized types

* Add test for type aliases, optimize to reuse adapters if possible

This is a tiny optimization to make type aliases (which did already work) reuse adapter properties if they already exist for the backing type. What this means is that if you have:

typealias Foo = String

and properties
foo: Foo
bar: String

you'll only get one adapter property field for String, and both will use it

* Use string templating where possible

* Remove all the kotshi tests
2018-03-11 21:17:55 -04:00
Eric Cochran
e6c2ebedde Disallow null annotation set in adapter lookup. (#460) 2018-03-10 06:42:07 -05:00
Eric Cochran
8e28dd4ad7 Fail earlier for some incorrect owner types. (#450) 2018-02-24 04:46:42 -05:00
Eric Cochran
ed1ea5a755 JsonAdapter.fromJson(String) must fully consume. (#441)
* JsonAdapter.fromJson(String) must fully consume.

* Replace field with method.
2018-02-21 20:15:38 -05:00
Eric Cochran
aede26d5e1 Clarify negation in condition. (#444) 2018-02-18 06:56:29 -05:00
Eric Cochran
3b89cf1fcb Fix ClassJsonAdapter to handle ParameterizedTypes. (#422) 2018-02-17 22:56:05 -05:00
Jake Wharton
bc7d849362 Reduce visual complexity of branching. 2018-02-13 15:04:34 -05:00
Jesse Wilson
5ad9d31bd8 Move modules into their own packages.
This sets the Automatic-Module-Name for moshi, moshi-adapters, and moshi-kotlin.
It moves moshi-adapters into its own .adapters package and forwards the existing
adapter. It moves the moshi-kotlin into its own .kotlin package and forwards the
existing adapter.

I'm not certain this is necessary or sufficient, but I think it's the right idea
for JPMS compatibility.
2018-02-07 04:41:06 -05:00
Eric Cochran
d26b2a151f Fix error message for invalid toJson signature. (#431)
void toJson(JsonWriter, JsonAdapter) does not make sense. This looks like my copy-paste error.
2018-02-03 23:50:22 -05:00
Eric Cochran
dba2f05b13 Improve error message for local classes. (#423) 2018-01-10 21:56:37 -05:00
Jesse Wilson
359244e996 Fix JsonValueReader to support up to 255 levels of nesting. (#417)
Follow up to https://github.com/square/moshi/pull/349
2018-01-07 14:55:02 -05:00
Eric Cochran
a210d89a55 Don't handle WildcardTypes in ClassJsonAdapter. (#406) 2018-01-07 14:05:49 -05:00
Jesse Wilson
0a6e836762 Support up to 255 levels of nesting. (#349)
Closes: https://github.com/square/moshi/issues/348
2018-01-07 12:17:00 -05:00
Eric Cochran
f53a77d311 Fix doc reference to Token. (#403)
JsonReader.Token is less redundant than Gson's JsonReader.JsonToken.
2017-12-01 12:07:26 -08:00
Eric Cochran
a8b1550e7e Make selectString consistent across JsonReaders. (#399)
Make JsonValueReader.selectString return -1 for non-strings instead of throwing.
2017-11-27 15:33:03 -08:00
Eric Cochran
f922371fa8 Let JsonValueReader.nextString read numbers. (#390)
* Let JsonValueReader.nextString read numbers.

This adds parity with JsonUtf8Reader and lets big number literals in JSON be read in as strings in Java.

* Remove trailing 0 in float literal.
2017-11-24 07:23:12 -05:00
Eric Cochran
2b7e5a3453 Add error message for accidental primitive usage.
Also, add a test for the requirement.
2017-11-11 18:37:04 -08:00
Eric Cochran
e643a04ee5 Add @CheckReturnValue to appropriate public APIs. 2017-11-04 21:13:07 -07:00
Jesse Wilson
8bf298ac14 Merge pull request #360 from square/eric.20171004.delegate-adapters
Allow delegates for intermediates in adapters.
2017-10-04 21:53:12 -04:00
Eric Cochran
4376a50f1f Clarify delegation with qualifiers test. (#359) 2017-10-04 16:49:23 -07:00
Eric Cochran
f847d47daa Remove Types.equal. (#358)
Nobody uses this helper method.
2017-10-04 15:54:17 -07:00
Eric Cochran
de336ef86e Allow delegates for intermediates in adapters. 2017-10-04 15:53:33 -07:00
Eric Cochran
2db89355f1 Add CheckReturnValue for toJson's string result.
This helps Error Prone and the IDE find accidental usages of toJson(value) instead of toJson(writer, value).
2017-09-25 14:18:31 -04:00
Eric Cochran
00694e9878 Fail earlier with null annotation set. 2017-07-19 11:29:06 -07:00
jwilson
9e9655b556 Change the adapter for Object.class to delegate.
Previously if we ever had an opaque Object, the content of this object
would always only use the built-in adapters for its members.

This changes the built-in Object adapter to do one layer of type checking
and then to delegate to user-supplied adapters.

The big upside of this is that application code can now change the default
numeric type to use when decoding an untyped object. Typically this will
be used to replace our default of Double with a user-specified numeric type
like BigDecimal.
2017-05-26 23:44:46 -04:00
jwilson
798f14bda5 [maven-release-plugin] prepare for next development iteration 2017-05-14 22:20:00 -04:00
jwilson
f42ae45f4c [maven-release-plugin] prepare release moshi-parent-1.5.0 2017-05-14 22:19:55 -04:00
Eric Cochran
e59dbf4f96 Add @Nullable to result of Types.nextAnnotations. (#298) 2017-05-06 21:07:59 -04:00
Jesse Wilson
c65b3bf1cb Import jsr305 and use it to mark @Nullable stuff. (#297) 2017-05-06 20:31:24 -04:00
jwilson
0ea1959b7e Enable Checkstyle 7.7. 2017-05-06 14:48:55 -04:00
Eric Cochran
13fd0b252c Throw NPE for null indent string in factory method. (#289)
Fail when creating the JsonAdapter rather than when using it.
2017-04-29 21:46:08 -04:00
Serj Lotutovici
f942e0fd52 Make Types.equals(Type, Type) public. (#292) 2017-04-29 21:45:06 -04:00
Eric Cochran
d95dd07c56 Fix Types.equals for arrays. (#279) 2017-04-20 17:27:47 -05:00