Question
· Apr 29, 2022

%JSON.Adaptor

Hi.

Is it possible to set in XData mappings (or any other way) to serialize the property value as display value rather than value it self?

Regards,
Matjaž

Product version: IRIS 2021.2
Discussion (5)1
Log in or sign up to continue

Hi.

Class code:

Class MasterData.Product Extends (%Persistent, %JSON.Adaptor)
{
Property Department As %String;
Property Ident As %String;
Index ProductId On (Department, Ident) [ IdKey, Unique ];
Property Name As %String;
Index Name On Name;
Property Type As %String(DISPLAYLIST = ",Sveže,Zmrznjeno,Dodelava", VALUELIST = ",FRE,FRO,FIN");
Property CUsInTU As %Integer;
Property NominalWeightCU As %Numeric;
Property WeightClassCU As %String;
Storage Default
{
<Data name="ProductDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>Name</Value>
</Value>
<Value name="3">
<Value>Type</Value>
</Value>
<Value name="4">
<Value>CUsInTU</Value>
</Value>
<Value name="5">
<Value>NominalWeightCU</Value>
</Value>
<Value name="6">
<Value>WeightClassCU</Value>
</Value>
</Data>
<DataLocation>^MasterData.ProductD</DataLocation>
<DefaultData>ProductDefaultData</DefaultData>
<IdLocation>^MasterData.ProductD</IdLocation>
<IndexLocation>^MasterData.ProductI</IndexLocation>
<StreamLocation>^MasterData.ProductS</StreamLocation>
<Type>%Storage.Persistent</Type>
}
XData RotorDataMapping
{
<Mapping xmlns="http://www.intersystems.com/jsonmapping">
<Property Name="Ident"/>
<Property Name="Name"/>
<Property Name="Type"/>
</Mapping>
}

I would like the property Type to be exported (%JSON.Export("RotorDataMapping") method) with display member rather than value member.

Regards,
Matjaž

Hiya,

Look at the %Library.String documentation.

You can use the JSONLISTPARAMETER
Property Type As %String(DISPLAYLIST ",Sveže,Zmrznjeno,Dodelava"VALUELIST ",FRE,FRO,FIN",JSONLISTPARAMETER=",Sveže,Zmrznjeno,Dodelava");

I have found that setting this parameter to the DISPLAYLIST parameter did not work, so I just put the same values in this parameter.

It does not seem like the XData block parameters for the JSON mappings supports something like that.
I did the following to get a different output value on the JSON. It is not the prettiest solution.

Class Test.JSONThings Extends (%Persistent, %XML.Adaptor, %JSON.Adaptor)
{

Property Name As %String;

Property Surname As %String;

Property Gender As %String(DISPLAYLIST = ",Male,Female", JSONLISTPARAMETER = "DISPLAYLIST", VALUELIST = ",M,F");

Property SomeOtherGender As %String(DISPLAYLIST = ",Man,Vrou", JSONLISTPARAMETER = "DISPLAYLIST", VALUELIST = ",M,F") [ Calculated, SqlComputeCode = { set {*}={Gender} }, SqlComputed, Transient ];

Property Age As %Integer; Property Notes As %String(MAXLEN = ""); Property Code As Test.JSONRef(%JSONREFERENCE = "ID");

XData NSMapping
{
<Mapping xmlns="http://www.intersystems.com/jsonmapping">
        <Property Name="Name" FieldName="Name"/>
        <Property Name="Surname" FieldName="Surname"/>
        <Property Name="SomeOtherGender" FieldName="Gender"/>
</Mapping>
}

}