Skip to content

Commit ac5fc85

Browse files
authored
Add NumberedEnum interface (#4769)
1 parent 8655160 commit ac5fc85

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

encoders/firebase-encoders-json/firebase-encoders-json.gradle

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
plugins {
1616
id 'firebase-library'
17+
id 'kotlin-android'
1718
}
1819

1920
firebaseLibrary {
@@ -24,16 +25,19 @@ firebaseLibrary {
2425
android {
2526
compileSdkVersion project.targetSdkVersion
2627
defaultConfig {
27-
minSdkVersion project.minSdkVersion
28-
targetSdkVersion project.targetSdkVersion
29-
versionName version
30-
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
28+
minSdkVersion project.minSdkVersion
29+
targetSdkVersion project.targetSdkVersion
30+
versionName version
31+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
3132
}
3233

3334
compileOptions {
3435
sourceCompatibility JavaVersion.VERSION_1_8
3536
targetCompatibility JavaVersion.VERSION_1_8
3637
}
38+
kotlinOptions {
39+
jvmTarget = '1.8'
40+
}
3741
testOptions {
3842
unitTests {
3943
includeAndroidResources = true

encoders/firebase-encoders-json/src/main/java/com/google/firebase/encoders/json/JsonValueObjectEncoderContext.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,11 @@ JsonValueObjectEncoderContext add(@Nullable Object o, boolean inline) throws IOE
317317

318318
// Process enum last if it does not have a custom encoder registered.
319319
if (o instanceof Enum) {
320-
add(((Enum) o).name());
320+
if (o instanceof NumberedEnum) {
321+
add(((NumberedEnum) o).getNumber());
322+
} else {
323+
add(((Enum<?>) o).name());
324+
}
321325
return this;
322326
}
323327

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.encoders.json
18+
19+
/** Represents an explicitly numbered enum for json serialization. */
20+
internal interface NumberedEnum {
21+
val number: Int
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.encoders.json
18+
19+
internal enum class DummyEnum(override val number: Int) : NumberedEnum {
20+
VALUE_1(1),
21+
VALUE_2(2),
22+
}

encoders/firebase-encoders-json/src/test/java/com/google/firebase/encoders/json/JsonValueObjectEncoderContextTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,18 @@ public void testEncodingEnum_withCustomEncoder() {
219219
assertThat(result).isEqualTo("[\"value_1\",\"value_2\"]");
220220
}
221221

222+
@Test
223+
public void testEncodingNumberedEnum() {
224+
String result =
225+
new JsonDataEncoderBuilder()
226+
.build()
227+
.encode(
228+
new DummyEnum[] {
229+
DummyEnum.VALUE_1, DummyEnum.VALUE_2, DummyEnum.VALUE_1, DummyEnum.VALUE_1
230+
});
231+
assertThat(result).isEqualTo("[1,2,1,1]");
232+
}
233+
222234
@Test
223235
public void testEncodingCollection() throws IOException {
224236
ObjectEncoder<InnerDummyClass> anotherObjectEncoder =

0 commit comments

Comments
 (0)