Skip to main content
Participant
March 21, 2019
Answered

Problem with enumerators in JSON

  • March 21, 2019
  • 2 replies
  • 1709 views

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

This topic has been closed for replies.
Correct answer grefel

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

2 replies

TWM73Author
Participant
March 21, 2019

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

Regards,

Tomek

grefel
Community Expert
grefelCommunity ExpertCorrect answer
Community Expert
March 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

Marc Autret
Legend
March 22, 2019

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

grefel
Community Expert
Community Expert
March 23, 2019

Thanks for the clarification Marc