Skip to content

Commit

Permalink
Support all struct types for Moshi json encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
quanturium authored and oldergod committed Feb 12, 2024
1 parent 86a2e90 commit 9169e63
Show file tree
Hide file tree
Showing 6 changed files with 1,208 additions and 28 deletions.
8 changes: 5 additions & 3 deletions gen-tests.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ val generateGsonAdapterCompactJavaTests by tasks.creating(JavaExec::class) {
"--proto_path=wire-tests/src/commonTest/shared/proto/proto2",
"--java_out=wire-gson-support/src/test/java",
"--compact",
"all_types_proto2.proto"
"all_types_proto2.proto",
)
}

Expand Down Expand Up @@ -135,11 +135,12 @@ val generateGsonAdapterKotlinTests by tasks.creating(JavaExec::class) {
"--proto_path=wire-tests/src/commonTest/shared/proto/proto2",
"--proto_path=wire-tests/src/commonTest/shared/proto/proto3",
"--kotlin_out=wire-gson-support/src/test/java",
"all_structs.proto",
"dinosaur_kotlin.proto",
"period_kotlin.proto",
"person_kotlin.proto",
"getters_kotlin.proto",
"pizza.proto"
"pizza.proto",
)
}

Expand Down Expand Up @@ -581,11 +582,12 @@ val generateMoshiAdapterKotlinTests by tasks.creating(JavaExec::class) {
"--proto_path=wire-tests/src/commonTest/shared/proto/proto2",
"--proto_path=wire-tests/src/commonTest/shared/proto/proto3",
"--kotlin_out=wire-moshi-adapter/src/test/java",
"all_structs.proto",
"person_kotlin.proto",
"dinosaur_kotlin.proto",
"period_kotlin.proto",
"getters_kotlin.proto",
"pizza.proto"
"pizza.proto",
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2024 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.wire

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.squareup.wire.json.assertJsonEquals
import org.junit.Test
import squareup.proto3.AllStructs

class GsonAllStructsTest {

@Test
fun kotlinFullObject() {
val json =
"""
|{
| "list": ["a1", "a2"],
| "mapInt32List": {"2":["c1", "c2"]},
| "mapInt32NullValue": {"1":null},
| "mapInt32Struct": {"1":{"key3":"value3"}},
| "mapInt32ValueA": {"1":{"key4":"value4"}},
| "oneofStruct": {"key2":"value2"},
| "struct": {"key1":"value1"}
|}
""".trimMargin()
val allStruct = gson.fromJson(json, AllStructs::class.java)
assertJsonEquals(json, gson.toJson(allStruct))
}

companion object {
private val gson: Gson = GsonBuilder()
.registerTypeAdapterFactory(WireTypeAdapterFactory())
.create()
}
}
Loading

0 comments on commit 9169e63

Please sign in to comment.