Article
Daniel Aguilar · Oct 12, 2022 2m read

New Feature %ZEN.proxyObject send numeric as String

Hi Developers!!,

I know what you are thinking... a new feature for ZEN.proxyObject...? NOW..???

In Spain we say that better late than never  ;-)

Have you ever need to send a numeric attribue of a Json in String format?

Did you go crazy casting class objects with fixed typed properties?

Lucky you!!

With this new feature I propose a way to continue working with our loved dynamic object %ZEN.proxyObject, being able to choose whether or not we want to send numeric attributes in String format.

You: "That's great!! but come on, stop rambling and explain to me how it works!!!."

Me: "That's the best part, it's so simple even a monkey could use it (and if a monkey can..., you can too!!).

          Just add an "x" in your call myObject.%ToJSON(,"x")

          And magically all numeric values will be converted to Strings!!!:"

You: "Are you kidding me? But what if I want to convert only one attribute?"

Me:  "No problem for that reason I have added 2 new properties:

  • forceStringFormat 
  • excludeStringformat

        Both properties are lists, so simply, add the name of the property that you want to translate to forceStringFormat OR exclueStringformat ant it will be forced OR ignored to convert the numeric value to String."

 

Example:

CODE:

ClassMethod testJson() As %Status
{
	#Dim objectA As %ZEN.proxyObject
	Set objectA=##class(%ZEN.proxyObject).%New()
	
	Set objetoB=##class(%ZEN.proxyObject).%New()
	Set objetoB.phone2=961365378
	Set objetoB.name="Dani"
	Set objectA.objetoB=objetoB
	Set objectA.phone=964121214
	u 0 w "NORMAL",!
	
	Do objectA.%ToJSON()
	
	U 0 W "",!,!
	
	u 0 w "WITH PARAMETER",!
	Do objectA.%ToJSON(,"aelotwux")
	
	U 0 W "",!,!
	
	u 0 w "WITH PARAMETRO AND EXCLUDED",!
	Do objectA.excludeStringformat.Insert("phone")
	Do objectA.%ToJSON(,"aelotwux")
	
	U 0 W "",!,!
	
	u 0 w "WITH FORCED LIST",!
	Do objectA.forceStringFormat.Insert("phone")
	Do objectA.%ToJSON()
}

RESULT:

NORMAL
{
        "objetoB": {
                "name":"Dani",
                "phone2":961365378
        },
        "phone":964121214
}
 
WITH PARAMETER
{
        "objetoB": {
                "name":"Dani",
                "phone2":"961365378"
        },
        "phone":"964121214"
}
 
WITH PARAMETRO AND EXCLUDED
{
        "objetoB": {
                "name":"Dani",
                "phone2":"961365378"
        },
        "phone":964121214
}
 
WITH FORCED LIST
{
        "objetoB": {
                "name":"Dani",
                "phone2":961365378
        },
        "phone":"964121214"
}

I hope it helps you, although help may be late ;-)

Thanks for reading me!.

I leave you a link to the GitHub repository from wich you can download the 2 modified xml to import them directly by dragging them to the Studio (for example).

https://github.com/DanielAguilarGarcia/CustomZenProxy
 

2
0 186
Discussion (1)2
Log in or sign up to continue