• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Problem with enumerators in JSON

New Here ,
Mar 21, 2019 Mar 21, 2019

Copy link to clipboard

Copied

I have a problem with generic style adding. I created a collection of objects in JSON that correspond to the style definition.

var myDef = {

   "mtPanelName": "superScript",

   "inddType": "Character",

   "parametersOfAddition": {

   "name": "Sup",

   "position": "Position.SUPERSCRIPT"

   }

app.activeDocument.characterStyles.add(myDef.parametersOfAddition);

But in JSON I can save the enumerator name only as a String. Unfortunately, most methods do not allow the use of a String. The problem generally concerns the storage of object names in JSON. I meet him more often. How can I solve it?

Regards,

Tomek

TOPICS
Scripting

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 21, 2019 Mar 21, 2019

Behind Position.SUPERSCRIPT is acutally a Number (in this case 1935831907) InDesign ExtendScript API (14.0) . Use this in your JSON. Simply omit the "

   "position": Position.SUPERSCRIPT

Votes

Translate

Translate
Community Expert ,
Mar 21, 2019 Mar 21, 2019

Copy link to clipboard

Copied

Behind Position.SUPERSCRIPT is acutally a Number (in this case 1935831907) InDesign ExtendScript API (14.0) . Use this in your JSON. Simply omit the "

   "position": Position.SUPERSCRIPT

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 22, 2019 Mar 22, 2019

Copy link to clipboard

Copied

Hi colleagues,

The important word in Gregor's answer is “behind”, as he writes, “Behind Position.SUPERSCRIPT is actually a Number (…)”

Indeed, keep in mind that although Position.SUPERSCRIPT can easily coerce into a Number, this is actually not a Number in CS5 and later. In fact, that's an object of the class Enumerator.

  // The truth 'behind' enumerator instances

  // ---

  alert( typeof Position.SUPERSCRIPT );       // => object

  alert( Position.SUPERSCRIPT.__class__ );    // => Enumerator

  alert( Position.SUPERSCRIPT.toString() );   // => "SUPERSCRIPT"

  alert( +Position.SUPERSCRIPT );             // => 1936749411

As you can see, a quick way to retrieve the underlying number of any enumeraor, myEnum, is to use the shortcut +myEnum (which implicitly involves the .valueOf() method.)

• More detail on this topic can be found in IdExtenso's enum extension.

The reason I emphasize this point here is, an Enumerator instance shouldn't appear as such in a JSON stream. Better is to numerify it (+myEnum) if no better option is available, because you can later assign that number to any DOM attribute that expects such enumerator and this will work fine. By contrast, if you assign the string reflection—i.e, myEnum.toString()—this won't work. In your JSON stream, there is good chance that the object Position.SUPERCRIPT will mutely translates into its associated string at some point and without your knowledge due to implicit stringification…

$$.JSON automatically coerces Enumerator instances into numbers. If you make it verbose, it provides graceful hints on the origin of that number:

// One of the advantages of using $$.JSON in InDesign

// ---

var obj = { myAttribute: Position.SUPERSCRIPT };

alert( $$.JSON(obj, 1/*verbose*/) );

// Output (stringified)

//----------------------

{

    "myAttribute" : 0x73707363 /* [spsc] */

}

Hope that helps.

@+

Marc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 23, 2019 Mar 23, 2019

Copy link to clipboard

Copied

Thanks for the clarification Marc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Mar 26, 2019 Mar 26, 2019

Copy link to clipboard

Copied

Now I know why you put those "+" everywhere

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 26, 2019 Mar 26, 2019

Copy link to clipboard

Copied

LATEST

Yep

Storing a number is not the same as storing an Object reference.

@+

Marc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 21, 2019 Mar 21, 2019

Copy link to clipboard

Copied

Thank you very much! That's what I was looking for.

Regards,

Tomek

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines