Class Structor
Structs and Values from POJOs. Useful for
converting Json data to Struct. For example:
Map<String, ?> jsonData = ...;
Struct struct = new Structor().struct(jsonData);
It can also be used to create a heterogeneous Struct literal like:
Struct ironMan = new Structor()
.struct(
"name", "Tony Stark",
"age", 10,
"known_as", List.of("Iron Man", "Genius"));
Or if your structs have many more fields, consider to use the toStruct()
BiCollector, as in:
BiStream.of("k1", 1, "k2", 2, "k3", 3, "k4", 4, ...)
.collect(new Structor().toStruct());
For simple scenarios, prefer to use MoreStructs to create Struct.
Its single-field struct() factory methods are more efficient, can be static imported,
and unsupported types cause compilation error as opposed to runtime exception.
The toValue(java.lang.Object) method is responsible for converting POJO to Value,
and recursively, the Collections, Maps and Tables thereof into
corresponding ListValue or Struct wrappers.
You can create a subclass to implement custom mapping. For example,
if the application needs to map User types to Value by using the user ids:
Structor customStructor = new Structor() {
public Value toValue(Object obj) {
if (obj instanceof User) { // custom logic
return toValue(((User) obj).getId());
}
return super.toValue(obj); // else delegate to default implementation
}
};
This custom mapping logic will be applied recursively to Iterable elements,
Map keys/values, and all other supported collection types.- Since:
- 5.8
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected com.google.protobuf.ValuedefaultValue(Object object) Called bytoValue(java.lang.Object)whenobjectcannot be converted.final com.google.protobuf.StructnestedStruct(Table<String, String, ?> table) Returns a nested Struct of Struct equivalent to therow mapoftable.final com.google.protobuf.Structstruct(CharSequence name, @Nullable Object value) final com.google.protobuf.Structstruct(CharSequence k1, @Nullable Object v1, CharSequence k2, @Nullable Object v2) Returns a Struct equivalent to{k1:v1, k2:v2}.final com.google.protobuf.Structstruct(CharSequence k1, @Nullable Object v1, CharSequence k2, @Nullable Object v2, CharSequence k3, @Nullable Object v3) Returns a Struct equivalent to{k1:v1, k2:v2, k3:v3}.final com.google.protobuf.Structstruct(CharSequence k1, @Nullable Object v1, CharSequence k2, @Nullable Object v2, CharSequence k3, @Nullable Object v3, CharSequence k4, @Nullable Object v4) Returns a Struct equivalent to{k1:v1, k2:v2, k3:v3, k4:v4}.final com.google.protobuf.Structstruct(CharSequence k1, @Nullable Object v1, CharSequence k2, @Nullable Object v2, CharSequence k3, @Nullable Object v3, CharSequence k4, @Nullable Object v4, CharSequence k5, @Nullable Object v5) Returns a Struct equivalent to{k1:v1, k2:v2, k3:v3, k4:v4, k5:v5}.final com.google.protobuf.Structstruct(CharSequence k1, @Nullable Object v1, CharSequence k2, @Nullable Object v2, CharSequence k3, @Nullable Object v3, CharSequence k4, @Nullable Object v4, CharSequence k5, @Nullable Object v5, CharSequence k6, @Nullable Object v6) Returns a Struct equivalent to{k1:v1, k2:v2, k3:v3, k4:v4, k5:v5, k6:v6}.final com.google.protobuf.StructReturns a Struct equivalent tomap.final BiCollector<CharSequence, Object, com.google.protobuf.Struct> toStruct()Returns aBiCollectorthat accumulates the name-value pairs into aStructwith the values converted usingtoValue(java.lang.Object).com.google.protobuf.ValueConvertsobjecttoValue.
-
Constructor Details
-
Structor
public Structor()
-
-
Method Details
-
struct
Returns a Struct withnameandvalue, withvalueconverted usingtoValue(java.lang.Object). In particular, null is mapped toNullValue.If runtime conversion error is undesirable, consider to use
MoreStructsor build Struct manually withStructBuilder.- Throws:
IllegalArgumentException- ifvaluecannot be convertedNullPointerException- ifnameis null
-
struct
public final com.google.protobuf.Struct struct(CharSequence k1, @Nullable Object v1, CharSequence k2, @Nullable Object v2) Returns a Struct equivalent to{k1:v1, k2:v2}.Values are converted using
toValue(java.lang.Object). In particular, null values are mapped toNullValue.If runtime conversion error is undesirable, consider to use
MoreStructsor build Struct manually withStructBuilder.- Throws:
IllegalArgumentException- if duplicate keys are provided or if either value cannot be convertedNullPointerException- if either key is null
-
struct
public final com.google.protobuf.Struct struct(CharSequence k1, @Nullable Object v1, CharSequence k2, @Nullable Object v2, CharSequence k3, @Nullable Object v3) Returns a Struct equivalent to{k1:v1, k2:v2, k3:v3}.Values are converted using
toValue(java.lang.Object). In particular, null values are mapped toNullValue.If runtime conversion error is undesirable, consider to use
MoreStructsor build Struct manually withStructBuilder.- Throws:
IllegalArgumentException- if duplicate keys are provided or if a value cannot be convertedNullPointerException- if any key is null
-
struct
public final com.google.protobuf.Struct struct(CharSequence k1, @Nullable Object v1, CharSequence k2, @Nullable Object v2, CharSequence k3, @Nullable Object v3, CharSequence k4, @Nullable Object v4) Returns a Struct equivalent to{k1:v1, k2:v2, k3:v3, k4:v4}.Values are converted using
toValue(java.lang.Object). In particular, null values are mapped toNullValue.If runtime conversion error is undesirable, consider to use
MoreStructsor build Struct manually withStructBuilder.- Throws:
IllegalArgumentException- if duplicate keys are provided or if a value cannot be convertedNullPointerException- if any key is null- Since:
- 5.9
-
struct
public final com.google.protobuf.Struct struct(CharSequence k1, @Nullable Object v1, CharSequence k2, @Nullable Object v2, CharSequence k3, @Nullable Object v3, CharSequence k4, @Nullable Object v4, CharSequence k5, @Nullable Object v5) Returns a Struct equivalent to{k1:v1, k2:v2, k3:v3, k4:v4, k5:v5}.Values are converted using
toValue(java.lang.Object). In particular, null values are mapped toNullValue.If runtime conversion error is undesirable, consider to use
MoreStructsor build Struct manually withStructBuilder.- Throws:
IllegalArgumentException- if duplicate keys are provided or if a value cannot be convertedNullPointerException- if any key is null- Since:
- 5.9
-
struct
public final com.google.protobuf.Struct struct(CharSequence k1, @Nullable Object v1, CharSequence k2, @Nullable Object v2, CharSequence k3, @Nullable Object v3, CharSequence k4, @Nullable Object v4, CharSequence k5, @Nullable Object v5, CharSequence k6, @Nullable Object v6) Returns a Struct equivalent to{k1:v1, k2:v2, k3:v3, k4:v4, k5:v5, k6:v6}.Values are converted using
toValue(java.lang.Object). In particular, null values are mapped toNullValue.If runtime conversion error is undesirable, consider to use
MoreStructsor build Struct manually withStructBuilder.- Throws:
IllegalArgumentException- if duplicate keys are provided or if a value cannot be convertedNullPointerException- if any key is null- Since:
- 5.9
-
struct
Returns a Struct equivalent tomap.Values are converted using
toValue(java.lang.Object). In particular, null values are mapped toNullValue.If runtime conversion error is undesirable, consider to use
MoreStructsor build Struct manually withStructBuilder.- Throws:
IllegalArgumentException- if a Map value cannot be convertedNullPointerException- if any key is null
-
nestedStruct
Returns a nested Struct of Struct equivalent to therow mapoftable.Values are converted using
toValue(java.lang.Object). In particular, null values are mapped toNullValue.If runtime conversion error is undesirable, consider to use
MoreStructsor build Struct manually withStructBuilder.- Throws:
IllegalArgumentException- if a Table cell value cannot be convertedNullPointerException- if any row key or column key is null
-
toStruct
Returns aBiCollectorthat accumulates the name-value pairs into aStructwith the values converted usingtoValue(java.lang.Object).Duplicate keys (according to
CharSequence.toString()) are not allowed.Null keys are not allowed, but null values will be mapped to
NullValue.If runtime conversion error is undesirable, consider to use
MoreStructsor build Struct manually withStructBuilder. -
toValue
ConvertsobjecttoValue. Must not return null.Supported types:
- Primitive types (boolean, number, string)
nullmapped toNullValue- Enum mapped to
name Iterableand array elements recursively converted and wrapped inListValueMapvalues recursively converted and wrapped inStructMultimapconverted astoValue(multimap.asMap())Tableconverted astoValue(table.rowMap())Optionalconverted astoValue(optional.orElse(null))ImmutableIntArray,ImmutableLongArrayandImmutableDoubleArrayelements wrapped inListValue- Built-in protobuf types (
Struct,Value,ListValue,NullValue)
-
defaultValue
Called bytoValue(java.lang.Object)whenobjectcannot be converted. Subclasses can override this method to throw a different exception type, or to return a catch-all defaultValue.- Throws:
IllegalArgumentException- to report that the type ofobjectisn't supported
-