Skip to main content

@MarshalledAs Enum

MarshalledAs is an enum in org.apache.juneau.marshall that specifies the marshalling strategy for a type annotated with @Marshalled. It is the value type of the @Marshalled(as) attribute.

Values​

ValueBehavior
DETECTAuto-detect the strategy (default). The marshalling engine inspects the type and picks the best representation — bean, map, collection, string swap, and so on.
STRINGSerialize using Object.toString() and parse using a fromString(String), valueOf(String), or single-String-argument constructor.

DETECT (default)​

DETECT is the default when as is not specified. It lets Juneau choose how to represent the type based on reflection — most classes are modeled as beans, Map/Collection types as objects/arrays, and types with a recognized string transform as strings. You rarely need to set this explicitly.

// Equivalent to no 'as' attribute at all.
@Marshalled(as=MarshalledAs.DETECT)
public class MyBean {
public String name;
public int age;
}

STRING​

MarshalledAs.STRING marshals the whole object as a single string. On serialize, Juneau calls toString(); on parse, it reconstructs the object from a fromString(String) static method, a valueOf(String) static method, or a single-String constructor. This is the declarative replacement for the former MarshallingStringSwap pattern — no separate swap class is required.

@Marshalled(as=MarshalledAs.STRING)
public class PhoneNumber {

private final String value;

// Single-String constructor used for parsing.
public PhoneNumber(String value) {
this.value = value;
}

// Used for serialization.
@Override
public String toString() {
return value;
}
}

Serialize / parse round-trip:

PhoneNumber p = new PhoneNumber("555-1234");

// Serializes to the toString() value: "555-1234"
String json = Json5.DEFAULT.of(p);

// Parses back via the single-String constructor.
PhoneNumber p2 = Json5.DEFAULT.to(json, PhoneNumber.class);

Because the object collapses to a single scalar, it appears inline wherever it is used — for example as a map key or a quoted value — rather than as a nested object.

Migration

Applying @Marshalled(as=MarshalledAs.STRING) to a class reproduces the pre-10.0 behavior in which @BeanIgnore on a class caused toString() serialization. See @MarshalledIgnore Annotation for the current @MarshalledIgnore semantics (which instead output null).

See Also
  • @Marshalled Annotation - The class-level annotation that uses this enum.
  • Swaps - The general-purpose object-transform mechanism for finer control.