public class JSONMarshaller extends Object implements Chainable<JSONMarshaller>, Function
protected static JSONMarshaller jsonMarsheller;
static {
//AppOrder.user is an interface. Define a default Impl class for it.
jsonMarsheller = new JSONMarshaller(AppOrder.class, null)
.chain(m -> m.setExcludedFields(Arrays.asList("orgRef"))
);
JsonConfig jc = jsonMarsheller.getConfig();
// Collections/Interface types match Classes for them
jc.setClassMap(ChainableWrapper.of(new HashMap())
.wrapperchain(m -> m.put("user", GuestConsumer.class))
.wrapperchain(m -> m.put("items", AppOrderItem.class))
.wrapperchain(m -> m.put("primaryItems", AppOrderItem.class))
.wrapperchain(m -> m.put("addOns", AppOrderAddOnItem.class))
//.wrapperchain(m -> m.put("promotionNames", ArrayList.class)) . NA as See #getPromotionNames Override
.get() );
// When AppOrder -> JSON. Then a derived object from items --> primaryItems is sent.
// So route all processing for primaryItems (phantom object) --> items (real field)
jc.registerJavaPropertyNameProcessor(AppOrder.class, new PropertyNameProcessor() {
Modifier and Type | Class and Description |
---|---|
static class |
JSONMarshaller.DisplayDateFormatJsonBeanProcessor
Standard Format is long and lousy : Convert to
DomainUtils.DATE_FORMAT_DISPLAY |
Modifier and Type | Field and Description |
---|---|
static net.sf.json.JsonConfig |
DATE_SERVER_DISPLAY_JSON_CONFIG
Prevents Recursive Data Structures + Handles Date as per
DomainUtils.DATE_FORMAT_DISPLAY on Server
TODO: Add unit test case |
static net.sf.json.util.NewBeanInstanceStrategy |
defaultNewBeanInstanceStrategy |
static net.sf.json.util.PropertyFilter |
filterNullPropertyFilter
Filter out null properties from JSON String
TODO: Add Unit test
|
static net.sf.json.JsonConfig |
NO_CYCLE_JSON_CONFIG
Prevents Recursive Data Structures
|
static net.sf.json.JsonConfig |
nullFilterConfig |
Constructor and Description |
---|
JSONMarshaller() |
JSONMarshaller(Class rootType,
Map<Class,JSONMarshaller> acceptedClasses) |
JSONMarshaller(net.sf.json.JsonConfig config) |
JSONMarshaller(List<String> acceptedFields,
Class rootType,
List<Class> acceptedClasses)
Deprecated.
|
JSONMarshaller(String[] acceptedFields,
Class rootType,
Class... acceptedClasses)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
Object |
apply(Object t) |
static String |
classObjectToJson(Object obj,
net.sf.json.JsonConfig config)
Ensures while marshalling the Class details are also there in the JSON result
|
static void |
convertToAttributes(Map attributes,
Function<net.sf.ezmorph.bean.MorphDynaBean,EntityAttribute> morpher)
Since Map attributes are hard to convert to a specific class type.
|
void |
decorateClassOverride(Map<Class,Function<net.sf.json.JSONObject,Object>> classTojsonObjectConsumer)
Override the class impl assumed by
JsonConfig with our own |
Map<Class,JSONMarshaller> |
getAcceptedClasses()
Map of Class to JSONMarshaller to nest another types filter configs. |
List<String> |
getAcceptedFields()
Fields to be included/accepted.
Fields that are neither included nor excluded will be filtered based on filterOnlyIfExcluded |
net.sf.json.JsonConfig |
getConfig() |
net.sf.json.JsonConfig |
getConfig(Class rootType) |
static net.sf.json.JsonConfig |
getConfig(Class rootType,
Class... excludedProperyTypes)
Gets a bare-bone non-nonsense config that can exclude nulls and certain types of properties
|
static net.sf.json.JsonConfig |
getConfigForNumberFields(Class rootType,
Map<String,Class<Number>> fieldTypesRequired)
JSON marshalling of Number types can be tricky; and while in JDK 1.8
JSONObject.toBean(JSONObject) is smart enough to introspect and determine the correct type,
in JDK 9, there are some issues and we have explicitly ensure the type is the intended type. |
List<String> |
getExcludedFields()
Fields to be excluded.
Fields that are neither included nor excluded will be filtered based on filterOnlyIfExcluded |
static <T,U> T |
getObjectFromDynaBean(org.apache.commons.beanutils.DynaBean dynaBean,
Class<U> claz)
Deprecated.
as it converts the
DynaBean to a string and does String manipulations; for objects with complex compositions this fails.
Example: Date conversions etc.Use JSONObject.toBean(JSONObject.fromObject(dynaBean), entityClass) directly. |
net.sf.json.util.PropertyFilter |
getPropertyFilter() |
void |
init() |
boolean |
isApplyFilter()
Filter un accepted fields out
TODO: May make the field redundant or deprecated
|
boolean |
isFilterNulls()
If false will NOT filter out nulls
|
boolean |
isFilterOnlyIfExcluded()
If true will only filter whats in the
excludedFields . |
static boolean |
isJSON(String json) |
static boolean |
isJSONArray(String json) |
static boolean |
isJSONObject(String json) |
void |
setAcceptedClasses(Map<Class,JSONMarshaller> acceptedClasses)
Map of Class to JSONMarshaller to nest another types filter configs. |
void |
setAcceptedFields(List<String> acceptedFields)
Fields to be included/accepted.
Fields that are neither included nor excluded will be filtered based on filterOnlyIfExcluded |
void |
setApplyFilter(boolean applyFilter)
Filter un accepted fields out
TODO: May make the field redundant or deprecated
|
void |
setExcludedFields(List<String> excludedFields)
Fields to be excluded.
Fields that are neither included nor excluded will be filtered based on filterOnlyIfExcluded |
void |
setFilterNulls(boolean filterNulls)
If false will NOT filter out nulls
|
void |
setFilterOnlyIfExcluded(boolean filterOnlyIfExcluded)
If true will only filter whats in the
excludedFields . |
public static net.sf.json.util.PropertyFilter filterNullPropertyFilter
public static net.sf.json.JsonConfig nullFilterConfig
public static net.sf.json.JsonConfig NO_CYCLE_JSON_CONFIG
public static net.sf.json.JsonConfig DATE_SERVER_DISPLAY_JSON_CONFIG
DomainUtils.DATE_FORMAT_DISPLAY
on Server
TODO: Add unit test casepublic static final net.sf.json.util.NewBeanInstanceStrategy defaultNewBeanInstanceStrategy
@Deprecated public JSONMarshaller(List<String> acceptedFields, Class rootType, List<Class> acceptedClasses)
JSONMarshaller(Class, Map)
@Deprecated public JSONMarshaller(String[] acceptedFields, Class rootType, Class... acceptedClasses)
JSONMarshaller(Class, Map)
public JSONMarshaller()
public JSONMarshaller(net.sf.json.JsonConfig config)
public JSONMarshaller(Class rootType, Map<Class,JSONMarshaller> acceptedClasses)
@PostConstruct public void init()
@Deprecated public static final <T,U> T getObjectFromDynaBean(org.apache.commons.beanutils.DynaBean dynaBean, Class<U> claz)
DynaBean
to a string and does String manipulations; for objects with complex compositions this fails.
Example: Date conversions etc.MorphDynaBean
holds JSON to a single ObjectT
- U
- dynaBean
- claz
- public net.sf.json.JsonConfig getConfig(Class rootType)
public static net.sf.json.JsonConfig getConfig(Class rootType, Class... excludedProperyTypes)
rootType
- excludedProperyTypes
- public static net.sf.json.JsonConfig getConfigForNumberFields(Class rootType, Map<String,Class<Number>> fieldTypesRequired)
JSONObject.toBean(JSONObject)
is smart enough to introspect and determine the correct type,
in JDK 9, there are some issues and we have explicitly ensure the type is the intended type. By default it becomes Integer in JDK 9.rootType
- as Class
fieldTypesRequired
- Map of fieldId String to its required Number type.public net.sf.json.JsonConfig getConfig()
public net.sf.json.util.PropertyFilter getPropertyFilter()
public boolean isApplyFilter()
public void setApplyFilter(boolean applyFilter)
public List<String> getAcceptedFields()
filterOnlyIfExcluded
public void setAcceptedFields(List<String> acceptedFields)
filterOnlyIfExcluded
public List<String> getExcludedFields()
filterOnlyIfExcluded
public void setExcludedFields(List<String> excludedFields)
filterOnlyIfExcluded
public boolean isFilterNulls()
public void setFilterNulls(boolean filterNulls)
public Map<Class,JSONMarshaller> getAcceptedClasses()
Map
of Class to JSONMarshaller
to nest another types filter configs.
Value can be null in which case it means the Class is an acepted type without specific filtering details.public void setAcceptedClasses(Map<Class,JSONMarshaller> acceptedClasses)
Map
of Class to JSONMarshaller
to nest another types filter configs.
Value can be null in which case it means the Class is an acepted type without specific filtering details.public boolean isFilterOnlyIfExcluded()
excludedFields
. If false will only include whats in the acceptedFields
public void setFilterOnlyIfExcluded(boolean filterOnlyIfExcluded)
excludedFields
. If false will only include whats in the acceptedFields
public static String classObjectToJson(Object obj, net.sf.json.JsonConfig config)
Object
- objString
TODO: Add unit test casepublic static boolean isJSONArray(String json)
public static boolean isJSONObject(String json)
public static boolean isJSON(String json)
public void decorateClassOverride(Map<Class,Function<net.sf.json.JSONObject,Object>> classTojsonObjectConsumer)
JsonConfig
with our ownpublic static void convertToAttributes(Map attributes, Function<net.sf.ezmorph.bean.MorphDynaBean,EntityAttribute> morpher)
JsonConfig.setClassMap(Map)
; if its not already of desirec type.
By default MorphDynaBean
is put.Copyright © 2018. All rights reserved.