项目作者: slackhq

项目描述 :
An interop tool for safely mixing Moshi and Gson models in JSON serialization.
高级语言: Kotlin
项目地址: git://github.com/slackhq/moshi-gson-interop.git
创建时间: 2020-09-22T02:51:57Z
项目社区:https://github.com/slackhq/moshi-gson-interop

开源协议:Apache License 2.0

下载


moshi-gson-interop

A tool for bidirectional interop between Moshi and Gson.

This is targeted at codebases that are in the process of migrating from Gson to Moshi and not a
general-purpose tool that should be used for other reasons.

Usage

In order to properly link two Moshi and Gson instances, you must pass two complete
instances (i.e. not intended to newBuilder() later) to Moshi.interopWith() and use
the returned instances.

  1. val seedMoshi: Moshi = ...
  2. val seedGson: Gson = ...
  3. // Use the returned instances!
  4. val (moshi, gson) = seedMoshi.interopWith(seedGson)

By default, the interop mechanism will attempt to best-guess which serializer to use based on a combination
of things (see DefaultMoshiClassChecker‘s kdoc). You can alternatively provide your own logic for this.

Advanced

You can use the Moshi.interopBuilder() extension to create an InteropBuilder for advanced usage and adding
your own ClassChecker logic.

Installation

Maven Central

  1. dependencies {
  2. implementation("com.slack.moshi:gson-interop:<version>")
  3. }

Snapshots of the development version are available in Sonatype’s snapshots repository.

Caveats

  • JsonQualifiers on a type in a Gson class will not be passed on to Moshi, as Gson (and the various
    tools around it) don’t capture them.
  • Performance
    • A Moshi adapter delegating to Gson should have no performance issues as it is able
      to stream data directly.
    • Streaming from Gson to Moshi will, however, be degraded as Gson has no equivalent
      streaming APIs to Moshi’s nextSource() or valueSink() APIs. This means that it
      must either decode the entire source into an intermediary object (i.e. a Map, List,
      etc) first when reading or encode the entire value into a JSON String first when writing.

License

  1. Copyright 2020 Slack Technologies, LLC
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.