1# json.tcl --
2#
3#	The json import plugin. Bridge between import management and
4#	the parsing of json markup.
5#
6# Copyright (c) 2009 Andreas Kupries <andreas_kupries@sourceforge.net>
7#
8# See the file "license.terms" for information on usage and redistribution
9# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10#
11# RCS: @(#) $Id: import_json.tcl,v 1.4 2009/11/23 23:04:25 andreas_kupries Exp $
12
13# This package is a plugin for the the doctools::idx v2 system.  It
14# takes text in json format and produces the list serialization of a
15# keyword index.
16
17# ### ### ### ######### ######### #########
18## Requisites
19
20# @sak notprovided dict
21# @mdgen NODEP: doctools::idx::import::plugin
22
23package require Tcl 8.4
24package require doctools::idx::import::plugin ; # The presence of this
25						# pseudo package
26						# indicates execution
27						# of this code inside
28						# of an interpreter
29						# which was properly
30						# initialized for use
31						# by import plugins.
32package require doctools::idx::structure      ; # Verification of the json
33					        # parse result as a
34					        # proper index
35					        # serialization.
36
37if {[package vcompare [package present Tcl] 8.5] < 0} {
38    if {[catch {
39	package require dict
40    }]} {
41	# Create a pure Tcl implementation of the dict methods
42	# required by json, and fake the presence of the dict package.
43	proc dict {cmd args} { return [uplevel 1 [linsert $args 0 dict/$cmd]] }
44	proc dict/create {} { return {} }
45	proc dict/set {var key val} {
46	    upvar 1 $var a
47	    array set x $a
48	    set x($key) $val
49	    set a [array get x]
50	    return
51	}
52	package provide dict 1
53    }
54}
55
56package require json ; # The actual json parser used by the plugin.
57# Requires 8.5, or 8.4+dict.
58
59# ### ### ### ######### ######### #########
60
61# ### ### ### ######### ######### #########
62## API :: Convert text to canonical index serialization.
63
64proc import {text configuration} {
65    # Note: We cannot fail here on duplicate keys in the input,
66    # especially for keywords and references, as we do for Tcl-based
67    # canonical index serializations, because our underlying JSON
68    # parser automatically merges them, by taking only the last found
69    # definition. I.e. of two or more definitions for a key X the last
70    # overwrites all previous occurences.
71    return [doctools::idx::structure canonicalize [json::json2dict $text]]
72}
73
74# ### ### ### ######### ######### #########
75## Ready
76
77package provide doctools::idx::import::json 0.1
78return
79