File tree 3 files changed +27
-3
lines changed
codecs/json-codec/src/main/java/software/amazon/smithy/java/json
3 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -141,6 +141,17 @@ public Builder defaultNamespace(String defaultNamespace) {
141
141
return this ;
142
142
}
143
143
144
+ /**
145
+ * Whether the type field should be written when Documents are being serialized. Default is true.
146
+ *
147
+ * @param serializeTypeInDocuments if the type field should be written when Documents are being serialized
148
+ * @return the builder
149
+ */
150
+ public Builder serializeTypeInDocuments (boolean serializeTypeInDocuments ) {
151
+ settingsBuilder .serializeTypeInDocuments (serializeTypeInDocuments );
152
+ return this ;
153
+ }
154
+
144
155
/**
145
156
* Uses a custom JSON serde provider.
146
157
*
Original file line number Diff line number Diff line change @@ -104,6 +104,15 @@ public String defaultNamespace() {
104
104
return defaultNamespace ;
105
105
}
106
106
107
+ /**
108
+ * Whether the type field should be written when Documents are being serialized. Default is true.
109
+ *
110
+ * @return true if the type field should be written when Documents are being serialized
111
+ */
112
+ public boolean serializeTypeInDocuments () {
113
+ return serializeTypeInDocuments ;
114
+ }
115
+
107
116
JsonSerdeProvider provider () {
108
117
return provider ;
109
118
}
@@ -118,6 +127,7 @@ void updateBuilder(Builder builder) {
118
127
if (fieldMapper instanceof JsonFieldMapper .UseJsonNameTrait ) {
119
128
builder .useJsonName (true );
120
129
}
130
+ builder .serializeTypeInDocuments (serializeTypeInDocuments );
121
131
}
122
132
123
133
/**
@@ -220,9 +230,10 @@ public Builder defaultNamespace(String defaultNamespace) {
220
230
}
221
231
222
232
/**
233
+ * Whether the type field should be written when Documents are being serialized. Default is true.
223
234
*
224
- * @param serializeTypeInDocuments
225
- * @return
235
+ * @param serializeTypeInDocuments if the type field should be written when Documents are being serialized
236
+ * @return the builder
226
237
*/
227
238
public Builder serializeTypeInDocuments (boolean serializeTypeInDocuments ) {
228
239
this .serializeTypeInDocuments = serializeTypeInDocuments ;
Original file line number Diff line number Diff line change @@ -292,7 +292,9 @@ private static final class SerializeDocumentContents extends SpecificShapeSerial
292
292
public void writeStruct (Schema schema , SerializableStruct struct ) {
293
293
try {
294
294
parent .generator .writeStartObject ();
295
- parent .generator .writeStringField ("__type" , schema .id ().toString ());
295
+ if (parent .settings .serializeTypeInDocuments ()) {
296
+ parent .generator .writeStringField ("__type" , schema .id ().toString ());
297
+ }
296
298
struct .serializeMembers (parent .structSerializer );
297
299
parent .generator .writeEndObject ();
298
300
} catch (Exception e ) {
You can’t perform that action at this time.
0 commit comments