Tusky-App-Android/app/src/main/res
Christophe Beyls df7b11afc3
Replace Gson library with Moshi (#4309)
**! ! Warning**: Do not merge before testing every API call and database
read involving JSON !

**Gson** is obsolete and has been superseded by **Moshi**. But more
importantly, parsing Kotlin objects using Gson is _dangerous_ because
Gson uses Java serialization and is **not Kotlin-aware**. This has two
main consequences:

- Fields of non-null types may end up null at runtime. Parsing will
succeed, but the code may crash later with a `NullPointerException` when
trying to access a field member;
- Default values of constructor parameters are always ignored. When
absent, reference types will be null, booleans will be false and
integers will be zero.

On the other hand, Kotlin-aware parsers like **Moshi** or **Kotlin
Serialization** will validate at parsing time that all received fields
comply with the Kotlin contract and avoid errors at runtime, making apps
more stable and schema mismatches easier to detect (as long as logs are
accessible):

- Receiving a null value for a non-null type will generate a parsing
error;
- Optional types are declared explicitly by adding a default value. **A
missing value with no default value declaration will generate a parsing
error.**

Migrating the entity declarations from Gson to Moshi will make the code
more robust but is not an easy task because of the semantic differences.

With Gson, both nullable and optional fields are represented with a null
value. After converting to Moshi, some nullable entities can become
non-null with a default value (if they are optional and not nullable),
others can stay nullable with no default value (if they are mandatory
and nullable), and others can become **nullable with a default value of
null** (if they are optional _or_ nullable _or_ both). That third option
is the safest bet when it's not clear if a field is optional or not,
except for lists which can usually be declared as non-null with a
default value of an empty list (I have yet to see a nullable array type
in the Mastodon API).

Fields that are currently declared as non-null present another
challenge. In theory, they should remain as-is and everything will work
fine. In practice, **because Gson is not aware of nullable types at
all**, it's possible that some non-null fields currently hold a null
value in some cases but the app does not report any error because the
field is not accessed by Kotlin code in that scenario. After migrating
to Moshi however, parsing such a field will now fail early if a null
value or no value is received.

These fields will have to be identified by heavily testing the app and
looking for parsing errors (`JsonDataException`) and/or by going through
the Mastodon documentation. A default value needs to be added for
missing optional fields, and their type could optionally be changed to
nullable, depending on the case.

Gson is also currently used to serialize and deserialize objects to and
from the local database, which is also challenging because backwards
compatibility needs to be preserved. Fortunately, by default Gson omits
writing null fields, so a field of type `List<T>?` could be replaced
with a field of type `List<T>` with a default value of `emptyList()` and
reading back the old data should still work. However, nullable lists
that are written directly (not as a field of another object) will still
be serialized to JSON as `"null"` so the deserializing code must still
be handling null properly.

Finally, changing the database schema is out of scope for this pull
request, so database entities that also happen to be serialized with
Gson will keep their original types even if they could be made non-null
as an improvement.

In the end this is all for the best, because the app will be more
reliable and errors will be easier to detect by showing up earlier with
a clear error message. Not to mention the performance benefits of using
Moshi compared to Gson.

- Replace Gson reflection with Moshi Kotlin codegen to generate all
parsers at compile time.
- Replace custom `Rfc3339DateJsonAdapter` with the one provided by
moshi-adapters.
- Replace custom `JsonDeserializer` classes for Enum types with
`EnumJsonAdapter.create(T).withUnknownFallback()` from moshi-adapters to
support fallback values.
- Replace `GuardedBooleanAdapter` with the more generic `GuardedAdapter`
which works with any type. Any nullable field may now be annotated with
`@Guarded`.
- Remove Proguard rules related to Json entities. Each Json entity needs
to be annotated with `@JsonClass` with no exception, and adding this
annotation will ensure that R8/Proguard will handle the entities
properly.
- Replace some nullable Boolean fields with non-null Boolean fields with
a default value where possible.
- Replace some nullable list fields with non-null list fields with a
default value of `emptyList()` where possible.
- Update `TimelineDao` to perform all Json conversions internally using
`Converters` so no Gson or Moshi instance has to be passed to its
methods.
- ~~Create a custom `DraftAttachmentJsonAdapter` to serialize and
deserialize `DraftAttachment` which is a special entity that supports
more than one json name per field. A custom adapter is necessary because
there is not direct equivalent of `@SerializedName(alternate = [...])`
in Moshi.~~ Remove alternate names for some `DraftAttachment` fields
which were used as a workaround to deserialize local data in 2-years old
builds of Tusky.
- Update tests to make them work with Moshi.
- Simplify a few `equals()` implementations.
- Change a few functions to `val`s
- Turn `NetworkModule` into an `object` (since it contains no abstract
methods).

Please test the app thoroughly before merging. There may be some fields
currently declared as mandatory that are actually optional.
2024-04-02 21:01:04 +02:00
..
anim improve MainActivity / LoginActivity transitions (#4301) 2024-03-09 11:04:29 +01:00
color 2512: Set style properly 2023-10-11 21:00:59 +02:00
drawable use link icon instead of emoji when showing hidden urls (#4031) 2023-09-26 21:46:05 +02:00
layout Replace Gson library with Moshi (#4309) 2024-04-02 21:01:04 +02:00
layout-land Fix trending tags being cut off (#3745) 2023-06-15 11:27:57 +02:00
layout-sw640dp Officially rename error elephant to errorphant (#3922) 2023-08-05 15:39:07 +02:00
menu Machine translation of posts (#4307) 2024-03-09 16:12:18 +01:00
mipmap-anydpi-v26 new app icon (#2695) 2022-09-13 19:48:09 +02:00
raw Update gradle, kotlin and other dependencies (#2291) 2022-01-20 21:10:32 +01:00
values Machine translation of posts (#4307) 2024-03-09 16:12:18 +01:00
values-ar Update translation files 2024-02-23 13:22:59 +00:00
values-be Translated using Weblate (Belarusian) 2024-03-27 09:57:03 +00:00
values-ber Replace “status” by “post” in strings name and source values (#2405) 2022-03-27 12:23:25 +02:00
values-bg Update translation files 2024-02-23 13:22:59 +00:00
values-bn-rBD Update translation files 2024-02-23 13:22:59 +00:00
values-bn-rIN Update translation files 2024-02-23 13:22:59 +00:00
values-ca Update translation files 2024-02-23 13:22:59 +00:00
values-ckb Update translation files 2024-02-23 13:22:59 +00:00
values-cs Update translation files 2024-02-23 13:22:59 +00:00
values-cy Translated using Weblate (English (United Kingdom)) 2024-03-27 09:57:03 +00:00
values-de Translated using Weblate (German) 2024-03-18 04:45:01 +00:00
values-el Translated using Weblate (Greek) 2023-07-29 12:53:03 +02:00
values-en-rAU Added translation using Weblate (French (Belgium)) 2023-10-25 04:45:14 +00:00
values-en-rGB Update translation files 2024-03-27 09:57:03 +00:00
values-eo Update translation files 2024-02-23 13:22:59 +00:00
values-es Update translation files 2024-02-23 13:22:59 +00:00
values-eu Update translation files 2024-02-23 13:22:59 +00:00
values-fa Update translation files 2024-02-23 13:22:59 +00:00
values-fi Update translation files 2024-02-23 13:22:59 +00:00
values-fr Update translation files 2024-02-23 13:22:59 +00:00
values-fr-rBE Added translation using Weblate (French (Belgium)) 2023-10-25 04:45:14 +00:00
values-fy Update translation files 2024-02-23 13:22:59 +00:00
values-ga Update translation files 2024-02-23 13:22:59 +00:00
values-gd Update translation files 2024-02-23 13:22:59 +00:00
values-gl Translated using Weblate (Galician) 2024-03-06 11:31:48 +00:00
values-hi Update translation files 2024-02-23 13:22:59 +00:00
values-hu Translated using Weblate (Hungarian) 2024-03-13 06:33:19 +00:00
values-in Update translation files 2024-02-23 13:22:59 +00:00
values-is Translated using Weblate (Icelandic) 2024-03-18 04:45:00 +00:00
values-it Update translation files 2024-02-23 13:22:59 +00:00
values-iw Added translation using Weblate (Hebrew) 2023-07-30 15:30:24 +02:00
values-ja Update translation files 2024-02-23 13:22:59 +00:00
values-kab Update translation files 2024-02-23 13:22:59 +00:00
values-ko Update translation files 2024-02-23 13:22:59 +00:00
values-large Theme refactoring (#1656) 2020-01-30 21:37:28 +01:00
values-large-land fix compose field on landscape tablets (#2747) 2022-11-07 19:56:28 +01:00
values-lb Added translation using Weblate (French (Belgium)) 2023-10-25 04:45:14 +00:00
values-lv Update translation files 2024-02-23 13:22:59 +00:00
values-ml Spelling (#2771) 2022-11-09 19:32:39 +01:00
values-nb-rNO Update translation files 2024-02-23 13:22:59 +00:00
values-night #3178: Increase contrast for separator lines in dark themes (#3497) 2023-03-30 19:31:23 +02:00
values-nl Update translation files 2024-02-23 13:22:59 +00:00
values-oc Update translation files 2024-02-23 13:22:59 +00:00
values-or Translated using Weblate (Odia) 2023-05-09 15:44:10 +00:00
values-pa Added translation using Weblate (French (Belgium)) 2023-10-25 04:45:14 +00:00
values-pl Update translation files 2024-02-23 13:22:59 +00:00
values-pt-rBR Update translation files 2024-02-23 13:22:59 +00:00
values-pt-rPT Update translation files 2024-02-23 13:22:59 +00:00
values-ru Update translation files 2024-02-23 13:22:59 +00:00
values-sa Update translation files 2024-02-23 13:22:59 +00:00
values-si Update translation files 2024-02-23 13:22:59 +00:00
values-sk Update translation files 2024-02-23 13:22:59 +00:00
values-sl Update translation files 2024-02-23 13:22:59 +00:00
values-small Implement media tab (#430) 2017-11-05 22:32:36 +01:00
values-sv Translated using Weblate (Swedish) 2024-03-01 06:32:34 +00:00
values-sw380dp ComposeActivity refactor (#1541) 2019-12-19 19:09:40 +01:00
values-ta Update translation files 2024-02-23 13:22:59 +00:00
values-te Added translation using Weblate (French (Belgium)) 2023-10-25 04:45:14 +00:00
values-th Update translation files 2024-02-23 13:22:59 +00:00
values-tr Translated using Weblate (Turkish) 2024-03-27 09:57:03 +00:00
values-uk Translated using Weblate (Ukrainian) 2024-03-18 04:45:00 +00:00
values-v27 Android 12 support, update AndroidX libraries (#2367) 2022-03-09 20:50:23 +01:00
values-vi Translated using Weblate (Vietnamese) 2024-03-10 04:45:01 +00:00
values-w640dp Add trending tags (#3149) 2023-02-14 19:52:11 +01:00
values-zh-rCN Update translation files 2024-02-23 13:22:59 +00:00
values-zh-rHK Update translation files 2024-02-23 13:22:59 +00:00
values-zh-rMO Update translation files 2024-02-23 13:22:59 +00:00
values-zh-rSG Update translation files 2024-02-23 13:22:59 +00:00
values-zh-rTW Update translation files 2024-02-23 13:22:59 +00:00
xml Add entry for "or" to locales_config.xml and donottranslate.xml (#3584) 2023-05-11 12:54:51 +02:00