|
3 | 3 |
|
4 | 4 | package com.microsoft.applicationinsights.diagnostics.collection.json; |
5 | 5 |
|
6 | | -import com.azure.json.JsonSerializable; |
| 6 | +import com.azure.json.JsonProviders; |
| 7 | +import com.azure.json.JsonReader; |
| 8 | +import com.azure.json.JsonToken; |
7 | 9 | import com.azure.json.JsonWriter; |
8 | 10 | import com.microsoft.applicationinsights.alerting.aiconfig.AlertingConfig; |
| 11 | +import java.io.ByteArrayOutputStream; |
9 | 12 | import java.io.IOException; |
| 13 | +import java.util.Locale; |
10 | 14 |
|
11 | | -public class AlertApiModule implements JsonSerializable<AlertApiModule> { |
| 15 | +public class AlertApiModule { |
12 | 16 |
|
13 | | - @Override |
14 | | - public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { |
15 | | - jsonWriter.writeStartObject(); |
16 | | - addEnumConfig(jsonWriter, AlertingConfig.RequestFilterType.class); |
17 | | - addEnumConfig(jsonWriter, AlertingConfig.RequestAggregationType.class); |
18 | | - addEnumConfig(jsonWriter, AlertingConfig.RequestTriggerThresholdType.class); |
19 | | - addEnumConfig(jsonWriter, AlertingConfig.RequestTriggerThrottlingType.class); |
20 | | - addEnumConfig(jsonWriter, AlertingConfig.RequestAggregationType.class); |
21 | | - jsonWriter.writeEndObject(); |
22 | | - return jsonWriter; |
| 17 | + public AlertApiModule() { |
| 18 | + addEnumConfig(AlertingConfig.RequestFilterType.class); |
| 19 | + addEnumConfig(AlertingConfig.RequestAggregationType.class); |
| 20 | + addEnumConfig(AlertingConfig.RequestTriggerThresholdType.class); |
| 21 | + addEnumConfig(AlertingConfig.RequestTriggerThrottlingType.class); |
| 22 | + addEnumConfig(AlertingConfig.RequestAggregationType.class); |
| 23 | + } |
| 24 | + |
| 25 | + private <T extends Enum<T>> void addEnumConfig(Class<T> clazz) { |
| 26 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 27 | + JsonWriter jsonWriter = JsonProviders.createWriter(outputStream); |
| 28 | + JsonReader jsonReader = JsonProviders.createReader(outputStream.toByteArray())) { |
| 29 | + addSerializer(jsonWriter, clazz); |
| 30 | + addDeserializer(jsonReader, clazz); |
| 31 | + } catch (IOException ignored) { |
| 32 | + // Ignored |
| 33 | + } |
23 | 34 | } |
24 | 35 |
|
25 | | - private <T extends Enum<T>> void addEnumConfig(JsonWriter jsonWriter, Class<T> clazz) |
| 36 | + private <T extends Enum<T>> void addSerializer(JsonWriter jsonWriter, Class<T> clazz) |
26 | 37 | throws IOException { |
27 | 38 | jsonWriter.writeStartObject(clazz.getSimpleName()); |
28 | 39 | for (T enumConstant : clazz.getEnumConstants()) { |
29 | 40 | jsonWriter.writeStringField( |
30 | | - enumConstant.name().toLowerCase().replace("_", "-"), enumConstant.name()); |
| 41 | + enumConstant.name().toLowerCase(Locale.ROOT).replace("_", "-"), enumConstant.name()); |
31 | 42 | } |
32 | 43 | jsonWriter.writeEndObject(); |
33 | 44 | } |
| 45 | + |
| 46 | + private <T extends Enum<T>> T addDeserializer(JsonReader jsonReader, Class<T> clazz) |
| 47 | + throws IOException { |
| 48 | + return jsonReader.readObject( |
| 49 | + reader -> { |
| 50 | + if (reader.nextToken() != JsonToken.END_OBJECT) { |
| 51 | + return Enum.valueOf( |
| 52 | + clazz, jsonReader.getString().toUpperCase(Locale.ROOT).replace("-", "_")); |
| 53 | + } else { |
| 54 | + reader.skipChildren(); |
| 55 | + return null; |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
34 | 59 | } |
0 commit comments