Class Structor
Struct
s and Value
s 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 Collection
s, Map
s and Table
s 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
-
Method Summary
Modifier and TypeMethodDescriptionprotected com.google.protobuf.Value
defaultValue
(Object object) Called bytoValue(java.lang.Object)
whenobject
cannot be converted.final com.google.protobuf.Struct
nestedStruct
(com.google.common.collect.Table<String, String, ?> table) Returns a nested Struct of Struct equivalent to therow map
oftable
.final com.google.protobuf.Struct
struct
(CharSequence name, @Nullable Object value) 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}
.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}
.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}
.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}
.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}
.final com.google.protobuf.Struct
Returns a Struct equivalent tomap
.final BiCollector
<CharSequence, Object, com.google.protobuf.Struct> toStruct()
Returns aBiCollector
that accumulates the name-value pairs into aStruct
with the values converted usingtoValue(java.lang.Object)
.com.google.protobuf.Value
Convertsobject
toValue
.
-
Constructor Details
-
Structor
public Structor()
-
-
Method Details
-
struct
Returns a Struct withname
andvalue
, withvalue
converted usingtoValue(java.lang.Object)
. In particular, null is mapped toNullValue
.If runtime conversion error is undesirable, consider to use
MoreStructs
or build Struct manually withStructBuilder
.- Throws:
IllegalArgumentException
- ifvalue
cannot be convertedNullPointerException
- ifname
is 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
MoreStructs
or 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
MoreStructs
or 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
MoreStructs
or 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
MoreStructs
or 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
MoreStructs
or 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
MoreStructs
or build Struct manually withStructBuilder
.- Throws:
IllegalArgumentException
- if a Map value cannot be convertedNullPointerException
- if any key is null
-
nestedStruct
public final com.google.protobuf.Struct nestedStruct(com.google.common.collect.Table<String, String, ?> table) Returns a nested Struct of Struct equivalent to therow map
oftable
.Values are converted using
toValue(java.lang.Object)
. In particular, null values are mapped toNullValue
.If runtime conversion error is undesirable, consider to use
MoreStructs
or 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 aBiCollector
that accumulates the name-value pairs into aStruct
with 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
MoreStructs
or build Struct manually withStructBuilder
. -
toValue
Convertsobject
toValue
. Must not return null.Supported types:
- Primitive types (boolean, number, string)
null
mapped toNullValue
- Enum mapped to
name
Iterable
and array elements recursively converted and wrapped inListValue
Map
values recursively converted and wrapped inStruct
Multimap
converted astoValue(multimap.asMap())
Table
converted astoValue(table.rowMap())
Optional
converted astoValue(optional.orElse(null))
ImmutableIntArray
,ImmutableLongArray
andImmutableDoubleArray
elements wrapped inListValue
- Built-in protobuf types (
Struct
,Value
,ListValue
,NullValue
)
-
defaultValue
Called bytoValue(java.lang.Object)
whenobject
cannot 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 ofobject
isn't supported
-