@@ -368,28 +368,51 @@ default void writeByteBufSet(FriendlyByteBuf buffer, Set<T> toWrite) {
368368 toWrite .forEach (t -> this .toByteBuf (buffer , t ));
369369 }
370370
371+ /**
372+ * Read an optional value from a child JSON element. If the child JSON member does not exist an empty optional will
373+ * be used.
374+ *
375+ * @param json The parent JSON element to read from.
376+ * @param memberName The name of the child member to read.
377+ * @return An optional containing the value that was read.
378+ */
379+ default Optional <T > fromJSONOptional (JsonObject json , String memberName ) {
380+
381+ return this .fromJSONOptional (json .get (memberName ));
382+ }
383+
384+ /**
385+ * Reads an optional value from a JSON element. If the element is null an empty optional will be returned.
386+ * @param json The JSON to read data from.
387+ * @return An optional containing the value that was read.
388+ */
389+ default Optional <T > fromJSONOptional (@ Nullable JsonElement json ) {
390+
391+ return json != null ? Optional .ofNullable (this .fromJSON (json )) : Optional .empty ();
392+ }
393+
371394 /**
372395 * Writes an optional value to a JSON element. If the value is not present a null value will be returned.
373396 *
374397 * @param value The optional value to write.
375398 * @return The written JSON element. If the optional value was not present this will be null.
376399 */
377- default JsonElement fromJSONOptional (Optional <T > value ) {
400+ @ Nullable
401+ default JsonElement toJSONOptional (@ Nullable T value ) {
378402
379- return value . map ( this :: toJSON ). orElse ( null );
403+ return this . toJSONOptional ( Optional . ofNullable ( value ) );
380404 }
381405
382406 /**
383- * Read an optional value from a child JSON element. If the child JSON member does not exist an empty optional will
384- * be used.
407+ * Writes an optional value to a JSON element. If the value is not present a null value will be returned.
385408 *
386- * @param json The parent JSON element to read from.
387- * @param memberName The name of the child member to read.
388- * @return An optional containing the value that was read.
409+ * @param value The optional value to write.
410+ * @return The written JSON element. If the optional value was not present this will be null.
389411 */
390- default Optional <T > fromJSONOptional (JsonObject json , String memberName ) {
412+ @ Nullable
413+ default JsonElement toJSONOptional (Optional <T > value ) {
391414
392- return json . has ( memberName ) ? Optional . of ( this . fromJSON ( json . get ( memberName ))) : Optional . empty ( );
415+ return value . map ( this :: toJSON ). orElse ( null );
393416 }
394417
395418 /**
0 commit comments