src/main/java/fr/maif/json/JsonRead.java
@SuppressWarnings("unchecked")
    static <A, T> JsonRead<T> oneOf(JsonRead<A> argRead, Option<String> atPath, List<ReadCase<? extends A, ? extends T>> cases) {
        JsonSchema oneOfSchema = JsonSchema.oneOf(cases.map(ReadCase::jsonSchema));
        return JsonRead.ofRead(argRead.flatMap(r -> cases
                    .map(c -> (ReadCase<A, T>) c)
                    .flatMap(c -> atPath.map(p -> c.jsonRead(r).map(rr -> __(p, rr))).getOrElse(c.jsonRead(r)))
                    .headOption()
                    .getOrElse(() -> {
                        JsonRead<T> err = j -> JsResult.error(JsResult.Error.error("Not reader found for value", r));
                        return err;
                    })
                ),
                atPath.map(p -> (JsonSchema) JsonSchema.propertySchema(p, oneOfSchema)).getOrElse(oneOfSchema)
        );
    }
/**
     * oneOf is used to handle polymorphism. With oneOf you can use different reader based on the value of a discriminant type.
     *
     * For exemple if you have this json
     *
     * <pre>{@code
     *   {
     *       "type": "String",
     *       "value": "A text value in string"
     *   }
     * }</pre>
     * and this json
     * <pre>{@code
     *   {
     *       "type": "Integer",
     *       "value": 123456
     *   }
     * }</pre>
     *
     * You can deserialize with this code
     * <pre>{@code
     *
     * JsonRead<String> oneOfRead = JsonRead.oneOf(_string("type"), "value", List(
     *      caseOf("String"::equals, _string()),
     *      caseOf("Integer"::equals, _int().map(String::valueOf))
     * ));
     *
     * }</pre>
     *
     * The json schema generated will be a "oneOf" json schema
     *
     * @param argRead a reader to get the discriminant type
     * @param atPath a path to apply the readers from cases
     * @param cases a list of cases to choose the right reader
     * @param <A>
     * @param <T>
     * @return the json read.
     */
oneOf is used to handle polymorphism. With oneOf you can use different reader based on the value of a discriminant type.
For exemple if you have this json



You can deserialize with this code
{@code





The json schema generated will be a "oneOf" json schema


oneOf is used to handle polymorphism . With oneOf you can use different reader based on the value of a discriminant type . For exemple if you have this json You can deserialize with this code { @code The json schema generated will be a " oneOf " json schema
@SuppressWarnings("unchecked")
    static <A, T> JsonRead<T> oneOf(JsonRead<A> argRead, Option<String> atPath, List<ReadCase<? extends A, ? extends T>> cases) {
        JsonSchema oneOfSchema = JsonSchema.oneOf(cases.map(ReadCase::jsonSchema));
        return JsonRead.ofRead(argRead.flatMap(r -> cases
                    .map(c -> (ReadCase<A, T>) c)
                    .flatMap(c -> atPath.map(p -> c.jsonRead(r).map(rr -> __(p, rr))).getOrElse(c.jsonRead(r)))
                    .headOption()
                    .getOrElse(() -> {
                        JsonRead<T> err = j -> JsResult.error(JsResult.Error.error("Not reader found for value", r));
                        return err;
                    })
                ),
                atPath.map(p -> (JsonSchema) JsonSchema.propertySchema(p, oneOfSchema)).getOrElse(oneOfSchema)
        );
    }
@ SuppressWarnings ( "unchecked" ) static < A , T > JsonRead < T > oneOf ( JsonRead < A > argRead , Option < String > atPath , List < ReadCase < ? extends A , ? extends T > > cases ) { JsonSchema oneOfSchema = JsonSchema . oneOf ( cases . map ( ReadCase :: jsonSchema ) ) ; return JsonRead . ofRead ( argRead . flatMap ( r -> cases . map ( c -> ( ReadCase < A , T > ) c ) . flatMap ( c -> atPath . map ( p -> c . jsonRead ( r ) . map ( rr -> __ ( p , rr ) ) ) . getOrElse ( c . jsonRead ( r ) ) ) . headOption ( ) . getOrElse ( ( ) -> { JsonRead < T > err = j -> JsResult . error ( JsResult . Error . error ( "Not reader found for value" , r ) ) ; return err ; } ) ) , atPath . map ( p -> ( JsonSchema ) JsonSchema . propertySchema ( p , oneOfSchema ) ) . getOrElse ( oneOfSchema ) ) ; }
oneOf is used to handle polymorphism.
oneOf is used to handle polymorphism .