JTM import and export

jansc edited this page Sep 14, 2010 · 2 revisions

Importing a JTM topic map

JTM is a notation for the Topic Maps Data Model in JSON. In tmjs version 0.3.0 JTM import and export has been added. In its current version tmjs does not include code to convert from JSON to and from objects. You can use your favorite JavaScript utility library such as prototype.js to convert your data from JSON to a Object. This object can then be imported into you topic map with the TM.JTM.Reader class:

// Our topic map represented as JavaScript Object
// (Slighly modified example from the JTM specification at 
// http://www.cerny-online.com/jtm/1.0/)
var obj = {
  "version": "1.0",
  "item_type": "topicmap",
  "topics": [
   {
      "subject_identifiers": [
        "http://psi.topincs.com/movies/dear-wendy",
        "http://psi.semanticheadache.com/movies/dear-wendy"
      ],
      "names": [
        {
          "value": "Dear Wendy",
          "type": "si:http://psi.topincs.com/title",
          "scope": [
            "si:http://www.topicmaps.org/xtm/1.0/country.xtm#US",
            "si:http://www.topicmaps.org/xtm/1.0/country.xtm#DE"
          ]
        }
      ],
      "occurrences": [
        {
          "value": "2005",
          "type": "si:http://psi.topincs.com/publication-year",
          "datatype": "http://www.w3.org/2001/XMLSchema#gYear"
        }
      ]
    }
  ],
  "associations": [
    {
      "type": "si:http://psi.topicmaps.org/iso13250/model/type-instance",
      "roles": [
        {
          "player": "si:http://psi.topincs.com/movies/dear-wendy",
          "type": "si:http://psi.topicmaps.org/iso13250/model/instance"
        },
        {
          "player": "si:http://psi.topincs.com/movie",
          "type": "si:http://psi.topicmaps.org/iso13250/model/type"
        }
      ]
    }
  ]
};
reader = new TM.JTM.Reader(tm);
reader.fromObject(obj);

Now the topic map contains all topics and associations from the JTM object:

topic = tm.getTopicBySubjectIdentifier(tm.createLocator("http://psi.topincs.com/movies/dear-wendy"));
// do stuff with the topic object

Exporting JTM topic maps

Exporting a JTM topic map is as easy as importing. Create a new instance of the TM.JTM.Writer object and call its toObject() method:

writer = new TM.JTM.Writer(tm);
obj = writer.toObject(tm);
// In Firefox with firebug you can do this:
// console.dir(obj);

The reason that you have to pass the tm object twice is that toObject() will soon be able to take all kind of Construct objects as a parameter. This will allow you to export e.g. single topics or associations.

JTM Limitations

In the current implementation there is not support of fragment import/export. You can only import/export TopicMap objects. Another limitation is that there is limited merging of duplicates during import. If you import the same topic map twice, you’ll end up with duplicate associations, occurrences and names.

Also, the user has to deal with conversion from objects to JSON strings and vice-versa. This is currently out of the scope of tmjs. (Should this be added to tmjs? Comments are welcome!)

Home