#1 2022-12-02 09:29:55

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 506

Difference m1 to m2 with JSON Quotes

Lets start with the bottom - to explain the problem
on our mvc page we use mustache and Javascript - With m1 it works and with m2 not.

we have a mustache partial with this code:

<script>
    window.templates = {};
    window.templates.vtSettings = '{{VTSettings}}';
</script>

VTSettings has this value : '{myarray:["AB","CD","EF"]}' and is a parameter of our MVC View

  IMyWebextensionsApplication = interface(IMVCApplication)
...
    procedure MyPage(var VTSettings : Variant);

later in our View we call Javascript to get the Array Elements from myarray:

  let VTSetting = replaceAll(window.templates.vtSettings, '{myarray:', '{"myarray":');   // <-- my Workaround :)
  let settings = JSON.parse(replaceAll(VTSetting, '&quot;', '"'));

at this place in m1 window.templates.vtSettings has the value

{&quot;myarray&quot;:[&quot;AB&quot;,&quot;CD&quot;,&quot;EF&quot;]}

and in m2

{myarray:[&quot;AB&quot;,&quot;CD&quot;,&quot;EF&quot;]}

JSON.Parse throws an Exception with the m2 Version. I have to add the quotes.

I debugged to find the reason in m2 and think there is a problem with mormot.core.variants.JsonToAnyVariant and the later ToJSON


Rad Studio 12.1 Santorini

Offline

#2 2022-12-02 10:16:16

Chaa
Member
Registered: 2011-03-26
Posts: 244

Re: Difference m1 to m2 with JSON Quotes

Try this:

<script>
    window.templates = {};
    window.templates.vtSettings = '{{ToJson VTSettings}}';
</script>

Online

#3 2022-12-02 17:07:36

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,183
Website

Re: Difference m1 to m2 with JSON Quotes

Hint:
Add the /json chars at the end of the page URI to see the returned JSON just before it is interpreted by the Mustache template.

It would help you achieve your goal.

Offline

#4 2022-12-03 09:33:26

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 506

Re: Difference m1 to m2 with JSON Quotes

Hm - May be you didnt understand what i mean...
How is it possible to add the missing 2 &quot; to the m2 json ?
(i added ToJSON to my mustache line but same result)

@ab i do not understand what you mean with add /json to end of the page URI - What is "end of the page URI" ?

ty

this is generated in the html

    window.templates.vtSettings = '{myarray:[&quot;AB&quot;,&quot;CD&quot;,&quot;EF&quot;]}';

Last edited by itSDS (2022-12-03 09:38:54)


Rad Studio 12.1 Santorini

Offline

#5 2022-12-03 20:21:41

tbo
Member
Registered: 2015-04-20
Posts: 335

Re: Difference m1 to m2 with JSON Quotes

itSDS wrote:

(i added ToJSON to my mustache line but same result)

There are predefined Expression Helpers available for Mustache. But you can also write your own helpers. The effort is small. Something like this:

protected
  procedure ArticleSizeToTextShort(const pmcValue: Variant; out pmoResult: Variant);

procedure ...ArticleSizeToTextShort(const pmcValue: Variant; out pmoResult: Variant);
const
  ARTICLE_SIZE_STR: array[TArticleSize] of String = ('N', 'S', 'L', 'XL', 'XXL');
var
  size: Integer;
begin
  if VariantToInteger(pmcValue, size) then
    pmoResult := ARTICLE_SIZE_STR[TArticleSize(size)]
  else
    SetVariantNull(pmoResult);
end;

.RegisterExpressionHelpers(['ArticleSizeToTextShort'], [ArticleSizeToTextShort])

With best regards
Thomas

Offline

#6 2022-12-04 15:32:16

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 506

Re: Difference m1 to m2 with JSON Quotes

Ty Thomas, (i didn't recognize this option to write expression Helper before)

But my concern for this ticket is the difference between m1 and m2.
and if this difference is wanted or not.


Rad Studio 12.1 Santorini

Offline

#7 2022-12-04 20:56:04

tbo
Member
Registered: 2015-04-20
Posts: 335

Re: Difference m1 to m2 with JSON Quotes

itSDS wrote:

But my concern for this ticket is the difference between m1 and m2.
and if this difference is wanted or not.

I tested your source code in my Mustache Editor, which I described in this article in forum Delphi-PRAXIS.

With the following source code in the editor:

<script>
  let VTSetting = '{{Test.myarray}}';
  let settings = JSON.parse(VTSetting.replaceAll('&quot;', '"'));
  alert(settings[0] + '-' + settings[1] + '-' + settings[2]);
</script>

With this JSON in file Test.json:

{
  "myarray": ["AB", "CD", "EF"]
}

The Alert window shows me the expected result "AB-CD-EF".

With best regards
Thomas

Offline

#8 2022-12-05 11:11:22

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 506

Re: Difference m1 to m2 with JSON Quotes

Hi Thomas,

i tried it with your demo, but there is a internal difference to the mvc model i use.
The TDocVariantData in your case has the JSON as String and in mvc its a TDocVariantData which is converted to JSON without quotes - It's not the same...

As conclusion theres a difference passing JSON String to MVC from m1 to m2.


Rad Studio 12.1 Santorini

Offline

Board footer

Powered by FluxBB