Commit Graph

390 Commits

Author SHA1 Message Date
Jesse Wilson
d5d172c3bb Switch to the Vanniktech base plugin for publishing (#1450)
* Switch to the Vanniktech base plugin for publishing

Move configuration out of build.gradle files and into build.gradle.kts files.
Sign published builds.
Support publishing release builds from GitHub actions.

* Update build.gradle.kts

Co-authored-by: Zac Sweers <zac.sweers@gmail.com>

Co-authored-by: Zac Sweers <zac.sweers@gmail.com>
2021-12-08 23:35:49 -05:00
Jesse Wilson
3cbd5c327a Fail gracefully when a primitive value is absent. (#1445)
Without this we get an AssertionError, which is the wrong exception
type for a JSON schema mismatch:

    java.lang.AssertionError: java.lang.NullPointerException: Cannot invoke "java.lang.Number.intValue()" because the return value of "sun.invoke.util.ValueConversions.primitiveConversion(sun.invoke.util.Wrapper, Object, boolean)" is null
      at com.squareup.moshi.RecordJsonAdapter.fromJson(RecordJsonAdapter.java:168)
      at com.squareup.moshi.internal.NullSafeJsonAdapter.fromJson(NullSafeJsonAdapter.java:41)
      at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:70)
      at com.squareup.moshi.records.RecordsTest.absentPrimitiveFails(RecordsTest.java:257)
    Caused by: java.lang.NullPointerException: Cannot invoke "java.lang.Number.intValue()" because the return value of "sun.invoke.util.ValueConversions.primitiveConversion(sun.invoke.util.Wrapper, Object, boolean)" is null
      at java.base/sun.invoke.util.ValueConversions.unboxInteger(ValueConversions.java:81)
      at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:732)
      at com.squareup.moshi.RecordJsonAdapter.fromJson(RecordJsonAdapter.java:156)
      ... 46 more
2021-12-04 19:10:35 -05:00
Jesse Wilson
75abba37ef Use functions in Util where possible (#1444) 2021-12-04 17:22:12 -05:00
Jesse Wilson
81bf3b1835 Don't wrap exceptions thrown by component adapters (#1443) 2021-12-04 16:38:56 -05:00
Jesse Wilson
cdcf0da4ee Fix a crash when combining generics and records (#1442)
Without the fix the test fails like so:

    No JsonAdapter for T (with no annotations)
    for T
    for java.util.List<T>
    for com.squareup.moshi.records.RecordsTest$IndirectGenerics<java.lang.Long>
    for class com.squareup.moshi.records.RecordsTest$HasIndirectGenerics
    java.lang.IllegalArgumentException: No JsonAdapter for T (with no annotations)
    for T
    for java.util.List<T>
    for com.squareup.moshi.records.RecordsTest$IndirectGenerics<java.lang.Long>
    for class com.squareup.moshi.records.RecordsTest$HasIndirectGenerics
      at com.squareup.moshi.Moshi$LookupChain.exceptionWithLookupStack(Moshi.java:389)
      at com.squareup.moshi.Moshi.adapter(Moshi.java:158)
      at com.squareup.moshi.Moshi.adapter(Moshi.java:106)
      at com.squareup.moshi.Moshi.adapter(Moshi.java:75)
2021-12-04 16:31:58 -05:00
Zac Sweers
751e8219e9 Fix code cleanups (#1441)
* Fix immutable properties

It was intentionally designed the previous way

* Restore limit in for loop

* Another ImmutableProperties
2021-11-30 21:56:00 -05:00
Zac Sweers
9f81ac897a Revert "Omit adapter field names now that we directly instantiate annotations (#1436)" (#1439)
This reverts commit 4a6cfb4d58.
2021-11-30 21:43:48 -05:00
Zac Sweers
fb8c7b50fc Deprecate Types.getFieldJsonQualifierAnnotations (#1440) 2021-11-30 21:43:39 -05:00
Zac Sweers
4a6cfb4d58 Omit adapter field names now that we directly instantiate annotations (#1436) 2021-11-30 16:23:47 -05:00
Tolriq
9ce039d637 Update proguard rules for R8 3.1
R8 3.1 in full mode have made improvements to annotation removal.
Current rules are not enough to ensure that JsonQualifier  annotatated class are kept.

See https://issuetracker.google.com/issues/206656057
2021-11-18 08:36:22 +01:00
Zac Sweers
954ca46b9e Add @Json.ignore (#1417)
* Default Json.name to an unset value

* Promote shared transient tests to DualKotlinTest

* Add new ignore property to Json

* Support it in ClassJsonAdapter

* Mention no enum/record support

* Support in KotlinJsonAdapter

* Rework code gen API to know of "ignored"

* Support in apt code gen

* Support in KSP

* Update old non-working transient example test

* Synthetic holders

* Use field on both
2021-11-08 11:16:57 -05:00
Zac Sweers
862ff7ddcd Update to JDK 17 (#1394)
Everything still targets JDK 8 (non-records) or JDK 16 (records only)
2021-10-16 02:18:51 -04:00
Zac Sweers
6947ab00d0 Modernize build a bit (#1383) 2021-08-23 17:09:59 -04:00
Zac Sweers
95250b0359 Implement reflective support for Java Records (#1381)
* Standardize around JDK 8

* Update GJF to support newer JDKs

* Fix misc java 8 issues in tests

* Prepare java 16/records checking at runtime

* Implement real RecordJsonAdapter

* Spotless

* Prepare build for JDK 16+

* Fix property name for kapt

* Small cleanup

* Make FallbackEnum java-8 happy

* Remove animalsniffer

* Fix format

* Add opens for ExtendsPlatformClassWithProtectedFields

* Return null every time in shim for main tests

* Use JDK 16 + release 8 to replace animalsniffer

* Simplify accessor accessible handling

* Remove manifest attrs

* Fix typo

* Fix KCT tests + upgrade it

* Cover another

* Try explicit kotlin daemon args for java 17?

* Disable 17-ea for now until kotlin 1.5.30

* Add JsonQualifier and Json(name) tests + fix qualifiers

* Ensure constructor is accessible

* GJF it properly

* GJF 1.11

* Unwrap InvocationTargetException

* Use MethodHandle for constructor

* Use MethodHandle for accessor too

* Revert "Remove manifest attrs"

This reverts commit 3eb768fd6904bb5c979aa01c3c182e0fb9329d62.

* Proper MR jar

* *actually* fix GJF, which wasn't getting applied before

We can just enable this everywhere now since we require JDK 16 anyway

* Make IDE happy about modules access

* Fixup records tests to play nice with modules

Gotta be public

* Add complex smoke test

* Remove comment

Not a regression test in this case
2021-08-23 12:00:06 -04:00
Goooler
2572c29e42 Code cleanups 2021-08-18 14:51:52 -04:00
Dkhusainov
3c108919ee JsonReader.nextSource javadoc (#1350)
Co-authored-by: Dinar <d.khusainov@infotech.team>
2021-05-31 16:35:32 -04:00
Zac Sweers
e343751593 Update to Kotlin 1.5 (and associated deps) (#1339) 2021-05-07 12:51:33 -04:00
Zac Sweers
b33a94dfc1 Update more dependencies (#1319) 2021-03-29 21:37:54 -04:00
Zac Sweers
797140c5cf Add some short basic docs to JsonAdapter methods (#1313) 2021-03-13 20:52:40 -05:00
Zac Sweers
6e5bb3a29b Prepare project for Kotlin migration (#1257) 2021-02-02 13:11:38 -05:00
Zac Sweers
230c3d801f Promote Kotlin type-inferring APIs to the main Moshi package (round 2!) (#1202)
* Make moshi-root a kotlin project

* Move moshi kotlin extensions to moshi core

* Add appropriate experimental annotations

* Add nextAdapter helper

* Add explicit return type on addAdapter

* Expression body for adapter

* Use nextAdapter helper

* Opportunistically fix a couple Util warnings

* Add Types extensions

* Spotless

* Use extensions in more places for added coverage

* Apply java versions on any java plugin type

This way the kotlin projects get this too

* Fix circularAdapters test?

* Use java 8 in java for code gen too

* Fixup with CircularAdaptersTest

* Add coverage for remaining

* Remove nextAdapter

* Remove leftover function

* Use asserts

left checkNotNull for the contract

* boxIfPrimitive

* Fixup docs

* Copyright fixes

* Add parameterized addAdapter

* Switch to using native javaType API

* Spotless

* Back to 2019

* Spotless

* Use rawType extension

* Fix rebase issues
2020-10-04 18:18:52 -04:00
Zac Sweers
f192473419 Standardize (almost) all tests on Truth (#1242)
* Replace assertj calls with Truth where possible

* Update test dependencies

* adapters

* reflect

* codegen

* moshi

* Add missing inOrder()

* Spotless
2020-09-26 20:38:39 -04:00
Zac Sweers
a1852a7315 Finish spotless license header configuration (#1231)
* Update spotless and enable copyright header bits again

* Reformat square copyright headers

* Create separate format configuration for external files

* Update CI to use java 15 final

* include stacktrace in ci failures

* Update ktlint to 0.39

* Disable java formatting on JDK 15

* Fix google header
2020-09-25 22:18:51 -04:00
Lachlan McKee
bf72ce8ade Introduced tags to reader/writers (#1227)
* Introduced tags to reader/writers

* Removed getTags method

* Added type safety to tag methods

* Changed to explicit class comparison

* Removed ? extends for tags

* Switched to LinkedHashMap

* Allowed polymorphism for tag values

* Simplified tags tests
2020-09-22 08:56:51 -04:00
Jesse Wilson
53c1bf0cac Fix problems discovered using nextSource() to implement skipValue (#1236) 2020-09-21 23:50:38 -04:00
Jesse Wilson
0a78ed4cb1 Moshi.Builder.addLast() (#1233)
This is mostly useful for KotlinJsonAdapterFactory.
2020-09-21 09:36:55 -04:00
Jesse Wilson
a09f0a5c26 Small improvements to JsonReader.nextSource
Defer doing I/O until strictly necessary.

Remove some unnecessary branching in doPeek(), which is in the
inner loop of all JSON parsing.
2020-09-14 22:05:27 -04:00
Jesse Wilson
6c01c34ecf New public API, JsonReader.nextSource()
The streaming side of https://github.com/square/moshi/issues/675

We can still add a nicer wrapper on top to read the JSON value as
a string.
2020-09-13 12:04:47 -04:00
Jesse Wilson
47d1037175 JsonValueSource: a brace-matching implementation of okio.Source
This will read one JSON object and then stop.

This is a part of https://github.com/square/moshi/issues/675
2020-09-12 22:37:03 -04:00
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