Commit Graph

311 Commits

Author SHA1 Message Date
Zac Sweers
56af4c3f89 Fix KotlinJsonAdapterFactory ref in error message (#1217) 2020-09-10 15:53:42 -04:00
Zac Sweers
5c07341977 Switch to Github Actions for CI 2020-09-01 15:48:33 -04:00
Zac Sweers
538890e8c0 Switch to spotless and format code (#1196)
* Add spotless configuration

* Reformat!

* Add copyright config for build.gradle.kts files

* Add toeholds for headers
2020-08-27 23:40:15 -04:00
Zac Sweers
4dd4a9d222 Migrate to Gradle (#1159)
* Start gradle root

* Clean up test API ambiguities

These don't compile in gradle anymore and the team[] warns

* Exclude .gradle dirs in git

* Set up moshi module

* Set up moshi-adapters

* Add repositories to subprojects

* Set target/source compatibility

* Set up examples

* Fix location of reflect/test dir

* Set up moshi-kotlin

* Set up code gen

* Opportunistic update code gen deps

* Fix up with code gen

* Set up kotlin tests

* Update snapshots

* Update travis build

* Configure checkstyle

* Cache gradle

* Finish fixing up checkstyle

* Now disable checkstyle until we can fix them all :|

* Update contributing

* Fix tests in codegen

* Remove unnecessary annotation

* Remove maven stuff!

* Suppress warning

* Remove jcenter

* Consolidate dependencies

* Revert "Clean up test API ambiguities"

This reverts commit 3ead69b844b5d7f66134b721e95581f5df1cccd6.

* Fix incap dep

* Opportunistically fix some small kotlinpoet deprecations

* Automatically apply the stdlib to all kotlin projects

* Opportunistic move to opt-in and remove unnecessary annotations

The kotlin maven plugin didn't handle these well in the IDE, gradle does

* Fix Type doc warning

* Fix okio version

* Fix dokka support

* Fix copypasta

* Use new snapshot

* Kotlin 1.4.0
2020-08-27 16:50:28 -04:00
Jesse Wilson
28831b04cf [maven-release-plugin] prepare for next development iteration 2020-08-26 21:38:44 -04:00
Jesse Wilson
cff20193c2 [maven-release-plugin] prepare release moshi-parent-1.10.0 2020-08-26 21:38:36 -04:00
Jesse Wilson
1dd9cd0b3b Make JsonReader.promoteNameToValue() a public API 2020-08-05 10:23:24 -04:00
Zac Sweers
aa18c6857c Expose getStrings() option on JsonReader.Options (#1180)
* Expose getStrings() option on JsonReader.Options

Resolves #1173

* Fix clone

Co-authored-by: Jake Wharton <github@jakewharton.com>

* Add mutation check

* Use set and make it unmodifiable

* Back to list

* strings()

* No clone()

Co-authored-by: Jake Wharton <github@jakewharton.com>
2020-07-30 23:10:41 -04:00
Tolriq
8c8c5977a7 R8 now have automatic detection of Class.forName and auto generate keep rules.
This makes application build with Moshi to keep unwanted Kotlin Metadata that have a huge impact on APK size.

After discussion with R8 team, there's currently no solution to avoid this other than using the trick in this PR.

Fixes https://github.com/square/moshi/issues/1115
2020-06-04 18:09:10 +02:00
Jake Wharton
db803ffa79 Merge pull request #1121 from angusholder/improve-collections-errors
Improve error when incorrectly trying to use a collection class like ArrayList instead of List
2020-04-09 21:23:41 -04:00
Graham Lea
d06758d092 Document thread safety 2020-04-09 21:22:49 -04:00
Angus Holder
2a9177168b Improved error when using collection classes by mistake
A new user of Moshi might try to make a class like

    data class Response (
        val users: ArrayList<User>
    )

Then when trying to create a JsonAdapter for it, they get an unhelpful error
message like

    Platform class java.util.ArrayList in java.util.ArrayList<java.lang.String>
    requires explicit JsonAdapter to be registered

which doesn't explain what they should do to fix the problem. In fact what it
hints towards is that the user should implement a JsonAdapter for ArrayList,
when really they just need to change the type to List!

Now the error looks like

    No JsonAdapter for java.util.ArrayList<java.lang.String>, you should use
    List instead of ArrayList (Moshi only supports the collection interfaces
    by default).
2020-04-09 21:19:13 -04:00
Jake Wharton
0790296cc7 Add JsonWriter.jsonValue API
This is symmetric with JsonReader.readJsonValue in that it writes an Object comprised of maps, lists, and some scalars.
2020-02-15 18:38:39 -05:00
Zac Sweers
debb7d3160 Generate proguard rules for code gen on demand (#1067)
* Extract target constructor signature into TargetConstructor.kt

We'll need this to know what the runtime types are for proguard rules

* Add ProguardConfig

* Wire in proguard config

* Write proguard configs out with adapters

* Add full tests

* Now remove the rules!

* Ignore on inline classes for now

* Pass adapter constructor params correctly
2020-01-14 14:59:10 -05:00
Zac Sweers
f891c8187b Check types parameter size in init (#1063)
* Add types size check in code gen

Resolves #932

* Add more useful message for mismatched generics lookup

* Add tests

* Style
2020-01-14 01:50:53 -05:00
Zac Sweers
0dfea712bb Always check for DefaultConstructorMarker class (#1038)
Reported by a Googler - We currently gate the lookup of the class on the presence of `Metadata`. However, `Metadata` might be justifiably stripped by Proguard/R8, even though `DefaultConstructorMarker` might still be present. This means Moshi's defaults invocation is likely broken on (at least) R8 builds without this.

Workaround until next release is to keep Metadata annotations
2019-11-24 22:52:59 -05:00
Jesse Wilson
b287596c60 [maven-release-plugin] prepare for next development iteration 2019-11-17 09:53:53 -05:00
Jesse Wilson
89a254d644 [maven-release-plugin] prepare release moshi-parent-1.9.2 2019-11-17 09:53:46 -05:00
Jesse Wilson
878e9a86d6 [maven-release-plugin] prepare for next development iteration 2019-10-30 23:26:50 -04:00
Jesse Wilson
0af27a39a6 [maven-release-plugin] prepare release moshi-parent-1.9.1 2019-10-30 23:26:43 -04:00
Zac Sweers
4f1c8a5eda Fix support for classes w/ multiple constructors in code gen (#976)
* Fix broken test

This test suite doesn't run on CI builds but fails locally since the method was moved

* Add multiple constructors test case

* Implement TypeName.asTypeBlock()

* Make DEFAULT_CONSTRUCTOR_MARKER public

* Look up constructor via getDeclaredConstructor with exact param types

Resolves #975

* Remove dead code
2019-10-30 00:39:08 -04:00
Jesse Wilson
4637950bab [maven-release-plugin] prepare for next development iteration 2019-10-29 07:57:56 -04:00
Jesse Wilson
0c8005bcee [maven-release-plugin] prepare release moshi-parent-1.9.0 2019-10-29 07:57:49 -04:00
Jesse Wilson
53b045eb86 Look up kotlin.Metadata::class only once 2019-10-26 23:59:27 -04:00
Jesse Wilson
eea2981fac Go back to using Unsafe APIs to create instances without constructors.
The attempted fix replaced a warning with a crash. I believe that a
warning is a better developer experience than a crash.

This reverts commit c0639316b1.
2019-10-26 22:02:49 -04:00
Justin Brooks
8460155dc5 Keep synthesized values() for EnumJsonAdapter 2019-10-26 12:20:11 -05:00
Jesse Wilson
2cf3f0204c Merge pull request #943 from square/jakew/value-sink/2019-09-27
Add JsonWriter.valueSink for streaming data
2019-10-17 23:45:38 -04:00
Zac Sweers
ae8b62b3ee Fix TypeVariable/WildcardType recursion causing stackoverflows (#948)
* Port Gson Types recursion fix for subtypeOf/supertypeOf

From a300148003

First step in resolving #338

* Port Gson Type resolve() recursion fix for type variables

From https://github.com/google/gson/pull/1128

Resolves #338

* Capture adapter to make checkstyle happy

* Move RecursiveTypesResolveTest out of internal package

* Use moshi convention for tests

* 2 is not 1
2019-10-15 18:01:48 -04:00
Jake Wharton
3816dbda74 Add JsonWriter.valueSink for streaming data
Unlike `value(BufferedSource)`, this retains the push-based nature of JsonWriter and allows layering other sinks (such as a base64-encoding Sink for binary data). Since `value(BufferedSource)` is trivially replaced by reading the source into the value sink, it is deprecated.
2019-10-10 10:18:06 -04:00
Zac Sweers
7804d74318 Report json name if different from property name in kotlin (#917)
* Report json name in code gen if different from property name

Resolves #800

* Unify required property name error message

* Report json name in kotlinjsonadapter, match code gen

* Upper case JSON

Co-Authored-By: Jake Wharton <jakew@google.com>

* Upper case JSON

Co-Authored-By: Jake Wharton <jakew@google.com>

* Don't keep constants

* Inline json name - property name comparison to util methods

* Remove unnecessary constructor keyword

* Consolidate non-null/missing property tests to parameterized suite

* Add custom json name tests for nonNull property checks

* Rename test to make maven happy

Maven won't run the test unless it ends with `Test` or `TestCase`
2019-09-30 23:04:21 -04:00
Jake Wharton
ecb7083abf Use char literals for array and object start/end 2019-09-30 14:40:12 -04:00
Jesse Wilson
52aaf9cc84 Merge pull request #933 from square/z/throwOnMissingTypeVars
Throw on missing type variables in Types.newParameterizedType* methods
2019-09-26 21:57:23 -04:00
Jake Wharton
12d37ffdc3 Point at the offending name when failing on unknown 2019-09-26 20:37:34 -04:00
Zac Sweers
96fe3e1417 Throw on missing type variables in Types.newParameterizedType* methods
It's a programmer error to omit this, but vararg declaration allows you to. This checks that.

We could improve it to ensure matching lengths or bounds, but this is a good start.
2019-09-24 18:27:36 -04:00
Zac Sweers
c0639316b1 Don't try to access unsafe APIs on android 29+ (#913)
* Don't try to access unsafe APIs on android 29+

Resolves #872. Not sure if we want to try caching some of these lookups

* Checkstyle

* Use correct inner class naming

Co-Authored-By: Jake Wharton <jakew@google.com>

* Name variable ignored

* Cache android check
2019-09-24 16:05:12 -04:00
Zac Sweers
a67b4d6a72 Inline mask generation and constructor invocation (#908)
* Differentiate local naming from properties with constructor defaults

* Remove constructor invocation and mask creation methods

We're inlining all this

* Add explanatory comment for why we use the default primitive value

* Inline mask generation and constructor invocation to generated adapters

* Remove unused argument

Co-Authored-By: Jake Wharton <jakew@google.com>

* Compute masks directly during code gen

* Opportunistic: remove extraneous double space from control flow

* Just mask and invert directly
2019-09-12 16:42:31 -04:00
Zac Sweers
71cf745852 Don't re-wrap nullsafe/nonnull adapters if they are already one (#909)
* Don't re-wrap nullsafe/nonnull adapters if they are already one

nullSafe() is often called defensively, which can lead to a lot of layers of wrapping and checking. This avoids that overhead where possible.

* Add tests
2019-09-12 14:14:49 -04:00
Zac Sweers
c820314107 Followups from #896 (#907)
* Simplify parameterProperties and parameterProperties

* Match only on synthetic constructors

* Limit synthetic constructor keeps to kotlin classes
2019-09-11 04:55:24 -04:00
Zac Sweers
619be26e95 Refactor missing/null properties to helper utils instead (#900)
* Create missing property and unexpected null templates

* Fix rebase conflicts
2019-09-08 17:36:58 -04:00
Zac Sweers
329d0e14b0 Invoke defaults constructor in kotlin code gen (#896)
* Add Util#invokeDefaultConstructor

* Add defaultPrimitiveValue

This will be used to initialize required properties for later invocation of the default constructor

* Move isTransient into PropertyGenerator

We will need this in order to know how to invoke constructors even if they have transient parameters

* Add notion of hasLocalIsPresentName to PropertyGenerator

* Switch to using invokeDefaultConstructor for any default property types

* Add code gen versions of default constructor test

* Fix mismatched names

* Use Arrays.copyOf

* Unwrap InvocationTargetException

* Use name allocator

* Rename createMask to createDefaultValuesParametersMask, use it directly

* Opportunistically clean up result variable holder

Only needs to be made if we have non-parameter instances, otherwise we can just return directly

* Fix mask name

* Remove unnecessary mod

* Switch to local lazily-initialized constructor reference

Not working because of some issue in kotlinpoet I don't understand

* Fix named usage

* Clean up debugging dots

* Add proguard/R8 rule for keeping defaults constructor in targets

* Make constructor lookup property private

* Add another defensive dot

* Rework invokeDefaultConstructor to accept vararg args

A little more idiomatic

* Update proguard rules
2019-09-08 17:24:25 -04:00
Zac Sweers
319d7c8bd5 Don't reflect on kotlinx and update doc
This adds kotlinx to the exclusion list, and also updates the doc on ClassJsonAdapter
2019-09-04 20:58:56 -04:00
Zac Sweers
9db91f2dce Kotlin-reflect QoL APIs (#875)
* Start MoshiKotlinExtensions

* Add Moshi.Builder.add() with reified typeOf

* Use non-shadowing name for addAdapter

* Implement KType.toType()

.javaType isn't implemented

* Don't re-wrap wrapped adapters

* Use new adapter functions in tests to smoke test

* Don't enable uses experimental in kotlin-reflect, only the test

* Use internal leniency API

* Use toType in addAdapter too

* Update error message in test
2019-09-04 20:24:59 -04:00
Zac Sweers
25fcbb2637 Improve message and match in tests 2019-07-08 12:40:55 -04:00
Zac Sweers
aea8675252 Pass raw type rather than possibly-generic type in platform type error
We only check the raw type here, so the message should match
2019-07-02 18:22:39 -04:00
Sye van der Veen
7e417840e2 Support covariant Map values, which are used by Kotlin 2019-05-28 10:51:55 -04:00
Zac Sweers
0943ef5a61 Allow custom generators (#847)
* Extract generatedJsonAdapterName to public API for other generators/consumers

* Fix kapt location in tests

* Add IDE-generated dependency-reduced-pom.xml to gitignore

This always bites me

* Add generator property to JsonClass and skip in processor

* Opportunistically fix formatting for generateAdapter doc

* Extract NullSafeJsonAdapter for delegate testing

* Add custom adapter tests

* Allow no-moshi constructors for generated adapters

* Fix rebase issue

* Use something other than nullSafe() for lenient check

This no longer propagates lenient

* Add missing copyrights

* Add top-level class note

* Add note about working against Moshi's generated signature

* Add missing bit to "requirements for"

* Note kotlin requirement relaxed in custom generators

* Style
2019-05-15 20:42:08 -04:00
Eric Cochran
d843950731 Make checkstyle happy on JsonReader column width. (#811) 2019-02-15 17:08:46 -08:00
Thomas Vos
f68035859e Fix typo in documentation for JsonReader.peekJson() (#797) 2019-02-15 11:15:38 -08:00
Eric Cochran
737f58cecb Unignore ObjectAdapterTest's toJsonCoercesRuntimeTypeForMaps 2018-11-26 17:04:40 -08:00
Zac Sweers
dbe7bfa15f Use faster isAnnotationPresent check
Followup from https://github.com/square/moshi/pull/749#discussion_r235224202
2018-11-20 17:20:09 -08:00