1212import com .fasterxml .jackson .annotation .JsonTypeName ;
1313import com .fasterxml .jackson .annotation .JsonUnwrapped ;
1414import com .fasterxml .jackson .annotation .JsonValue ;
15+ import com .fasterxml .jackson .core .JsonParser ;
16+ import com .fasterxml .jackson .databind .DeserializationContext ;
17+ import com .fasterxml .jackson .databind .JsonDeserializer ;
18+ import com .fasterxml .jackson .databind .JsonNode ;
19+ import com .fasterxml .jackson .databind .ObjectMapper ;
20+ import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
21+ import java .io .IOException ;
1522import java .lang .Object ;
1623import java .lang .String ;
1724import java .util .Objects ;
1825import java .util .Optional ;
1926
27+ @ JsonDeserialize (using = ChatMessageWithPlaceholders .Deserializer .class )
2028public final class ChatMessageWithPlaceholders {
2129 private final Value value ;
2230
@@ -199,6 +207,11 @@ private static final class _UnknownValue implements Value {
199207 private _UnknownValue (@ JsonProperty ("value" ) Object value ) {
200208 }
201209
210+ private _UnknownValue (String type , Object value ) {
211+ this .type = type == null ? "unknown" : type ;
212+ this .value = value == null ? "" : value ;
213+ }
214+
202215 @ java .lang .Override
203216 public <T > T visit (Visitor <T > visitor ) {
204217 return visitor ._visitUnknown (value );
@@ -211,7 +224,7 @@ public boolean equals(Object other) {
211224 }
212225
213226 private boolean equalTo (_UnknownValue other ) {
214- return type .equals (other .type ) && value .equals (other .value );
227+ return Objects .equals (type , other .type ) && Objects .equals (value , other .value );
215228 }
216229
217230 @ java .lang .Override
@@ -224,4 +237,48 @@ public String toString() {
224237 return "ChatMessageWithPlaceholders{" + "type: " + type + ", value: " + value + "}" ;
225238 }
226239 }
240+
241+ static final class Deserializer extends JsonDeserializer <ChatMessageWithPlaceholders > {
242+ @ java .lang .Override
243+ public ChatMessageWithPlaceholders deserialize (JsonParser p , DeserializationContext ctxt )
244+ throws IOException {
245+ ObjectMapper mapper = (ObjectMapper ) p .getCodec ();
246+ JsonNode node = mapper .readTree (p );
247+ if (node == null || node .isNull ()) {
248+ return null ;
249+ }
250+
251+ String type = textOrNull (node .get ("type" ));
252+ if ("chatmessage" .equals (type )) {
253+ ChatMessage message = mapper .treeToValue (node , ChatMessage .class );
254+ return ChatMessageWithPlaceholders .chatmessage (message );
255+ }
256+ if ("placeholder" .equals (type )) {
257+ PlaceholderMessage placeholder = mapper .treeToValue (node , PlaceholderMessage .class );
258+ return ChatMessageWithPlaceholders .placeholder (placeholder );
259+ }
260+
261+ boolean hasRole = node .has ("role" );
262+ boolean hasContent = node .has ("content" );
263+ boolean hasName = node .has ("name" );
264+ if (hasRole || hasContent ) {
265+ ChatMessage message = mapper .treeToValue (node , ChatMessage .class );
266+ return ChatMessageWithPlaceholders .chatmessage (message );
267+ }
268+ if (hasName ) {
269+ PlaceholderMessage placeholder = mapper .treeToValue (node , PlaceholderMessage .class );
270+ return ChatMessageWithPlaceholders .placeholder (placeholder );
271+ }
272+
273+ Object raw = mapper .treeToValue (node , Object .class );
274+ return new ChatMessageWithPlaceholders (new _UnknownValue (type , raw ));
275+ }
276+
277+ private String textOrNull (JsonNode node ) {
278+ if (node == null || node .isNull ()) {
279+ return null ;
280+ }
281+ return node .asText ();
282+ }
283+ }
227284}
0 commit comments