All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.t9t.jooq.json.JsonDSL Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
package com.github.t9t.jooq.json;

import org.jooq.Field;
import org.jooq.JSON;
import org.jooq.impl.DSL;

import java.util.Collection;

/**
 * 

Functions for {@code json} PostgreSQL operator support in jOOQ

* *

Reference: https://www.postgresql.org/docs/11/functions-json.html

*/ public final class JsonDSL { /** * Create a jOOQ {@link Field} wrapping a {@link JSON} object representing a {@code json} value for the JSON * string. Note that the JSON is not validated (any formatting errors will only occur when * interacting with the database). * * @param json JSON string * @return {@code json} {@code Field} for the JSON string */ public static Field field(String json) { return field(JSON.valueOf(json)); } /** * Create a jOOQ {@link Field} wrapping the {@link JSON} object. * * @param json {@code JSON} object to wrap * @return {@code json} {@code Field} for the {@code JSON} object */ public static Field field(JSON json) { return DSL.field("{0}", JSON.class, json); } /** *

Get JSON array element (indexed from zero, negative integers count from the end), using the * -> operator

* *

Example: '[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json->2

*

Example result: {"c":"baz"}

* * @param jsonField A JSON {@code Field} containing an array to get the array element from * @param index Array index; negative values count from the end * @return A {@code Field} representing the extracted array element */ public static Field arrayElement(Field jsonField, int index) { return DSL.field("{0}->{1}", JSON.class, jsonField, index); } /** *

Get JSON array element as {@code text} rather than {@code json(b)} (indexed from zero, negative integers * count from the end), using the ->> operator

* *

Example: '[1,2,3]'::json->>2

*

Example result: 3

* * @param jsonField A JSON {@code Field} containing an array to get the array element from * @param index Array index; negative values count from the end * @return A {@code Field} representing the extracted array element, as text */ public static Field arrayElementText(Field jsonField, int index) { return DSL.field("{0}->>{1}", String.class, jsonField, index); } /** *

Get JSON object field by key using the -> operator

* *

Example: '{"a": {"b":"foo"}}'::json->'a'

*

Example result: {"b":"foo"}

* * @param jsonField The JSON {@code Field} to extract the field from * @param key JSON field key name * @return A {@code Field} representing the extracted value */ public static Field fieldByKey(Field jsonField, String key) { return DSL.field("{0}->{1}", JSON.class, jsonField, key); } /** *

Get JSON object field as {@code text} rather than {@code json(b)}, using the ->> * operator

* *

Example: '{"a":1,"b":2}'::json->>'b'

*

Example result: 2

* * @param jsonField The JSON {@code Field} to extract the field from * @param key JSON field key name * @return A {@code Field} representing the extracted array element, as text */ public static Field fieldByKeyText(Field jsonField, String key) { return DSL.field("{0}->>{1}", String.class, jsonField, key); } /** *

Get JSON object at specified path using the #> operator

* *

Example: '{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}'

*

Example result: {"c": "foo"}

* * @param jsonField The JSON {@code Field} to extract the path from * @param path Path to the the object to return * @return A {@code Field} representing the object at the specified path * @see #objectAtPath(Field, Collection) */ public static Field objectAtPath(Field jsonField, String... path) { return DSL.field("{0}#>{1}", JSON.class, jsonField, DSL.array(path)); } /** *

Get JSON object at specified path using the #> operator

* *

Example: '{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}'

*

Example result: {"c": "foo"}

* * @param jsonField The JSON {@code Field} to extract the path from * @param path Path to the the object to return * @return A {@code Field} representing the object at the specified path * @see #objectAtPath(Field, String...) */ public static Field objectAtPath(Field jsonField, Collection path) { return objectAtPath(jsonField, path.toArray(new String[0])); } /** *

Get JSON object at specified path as {@code text} rather than {@code json(b)}, using the #>> * operator

* *

Example: '{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'

*

Example result: 3

* * @param jsonField The JSON {@code Field} to extract the path from * @param path Path to the the object to return * @return A {@code Field} representing the object at the specified path, as text * @see #objectAtPathText(Field, Collection) */ public static Field objectAtPathText(Field jsonField, String... path) { return DSL.field("{0}#>>{1}", String.class, jsonField, DSL.array(path)); } /** *

Get JSON object at specified path as {@code text} rather than {@code json(b)}, using the #>> * operator

* *

Example: '{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'

*

Example result: 3

* * @param jsonField The JSON {@code Field} to extract the path from * @param path Path to the the object to return * @return A {@code Field} representing the object at the specified path, as text * @see #objectAtPath(Field, String...) */ public static Field objectAtPathText(Field jsonField, Collection path) { return objectAtPathText(jsonField, path.toArray(new String[0])); } /** *

Returns the number of elements in the outermost JSON array.

* *

Example: json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]')

*

Example result: 5

* * @param jsonField The JSON {@code Field} containing an array to measure the length of * @return Length of the array */ public static Field arrayLength(Field jsonField) { return DSL.field("json_array_length({0})", Integer.class, jsonField); } /** *

Returns JSON value pointed to by {@code path} (equivalent to #> operator, ie. * {@link #objectAtPath(Field, String...)}).

* *

Example: json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4')

*

Example result: {"f5":99,"f6":"foo"}

* * @param jsonField The JSON {@code Field} to extract the path from * @param path Path to the the object to return * @return A {@code Field} representing the object at the specified path * @see #objectAtPath(Field, String...) * @see #objectAtPath(Field, Collection) * @see #extractPath(Field, Collection) */ public static Field extractPath(Field jsonField, String... path) { return DSL.field("json_extract_path({0}, VARIADIC {1})", JSON.class, jsonField, DSL.array(path)); } /** *

Returns JSON value pointed to by {@code path} (equivalent to #> operator, ie. * {@link #objectAtPath(Field, Collection)}).

* *

Example: json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4')

*

Example result: {"f5":99,"f6":"foo"}

* * @param jsonField The JSON {@code Field} to extract the path from * @param path Path to the the object to return * @return A {@code Field} representing the object at the specified path * @see #objectAtPath(Field, String...) * @see #objectAtPath(Field, Collection) * @see #extractPath(Field, String...) */ public static Field extractPath(Field jsonField, Collection path) { return extractPath(jsonField, path.toArray(new String[0])); } /** *

Returns JSON value pointed to by {@code path} as text (equivalent to #>> operator, ie. * {@link #objectAtPathText(Field, String...)}).

* *

Example: json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6')

*

Example result: foo

* * @param jsonField The JSON {@code Field} to extract the path from * @param path Path to the the object to return * @return A {@code Field} representing the object at the specified path, as text * @see #objectAtPathText(Field, String...) * @see #objectAtPathText(Field, Collection) * @see #extractPathText(Field, Collection) */ public static Field extractPathText(Field jsonField, String... path) { return DSL.field("json_extract_path_text({0}, VARIADIC {1})", String.class, jsonField, DSL.array(path)); } /** *

Returns JSON value pointed to by {@code path} as text (equivalent to #>> operator, ie. * {@link #objectAtPathText(Field, Collection)}).

* *

Example: json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6')

*

Example result: foo

* * @param jsonField The JSON {@code Field} to extract the path from * @param path Path to the the object to return * @return A {@code Field} representing the object at the specified path, as text * @see #objectAtPathText(Field, String...) * @see #objectAtPathText(Field, Collection) * @see #extractPathText(Field, String...) */ public static Field extractPathText(Field jsonField, Collection path) { return extractPathText(jsonField, path.toArray(new String[0])); } /** *

Returns the type of the outermost JSON value as a text string. Possible types are {@code object}, {@code array}, * {@code string}, {@code number}, {@code boolean}, and {@code null}.

* *

Example: json_typeof('-123.4')

*

Example result: number

* * @param jsonField The JSON {@code Field} to determine the type of * @return The JSON type */ public static Field typeOf(Field jsonField) { return DSL.field("json_typeof({0})", String.class, jsonField); } /** *

Returns a JSON {@code Field} with all object fields that have {@code null} values omitted. Other {@code null} * values (eg. in arrays) are untouched.

* *

Example: json_strip_nulls('[{"f1":1,"f2":null},2,null,3]')

*

Example result: [{"f1":1},2,null,3]

* * @param jsonField The JSON {@code Field} to remove {@code null} values from * @return A JSON {@code Field} with {@code null} object fields removed */ public static Field stripNulls(Field jsonField) { return DSL.field("json_strip_nulls({0})", JSON.class, jsonField); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy