JSON alternatives (for the purpose of specifying configuration)?

后端 未结 8 2548
悲&欢浪女
悲&欢浪女 2021-02-19 20:31

I like json as a format for configuration files for the software I write. I like that it\'s lightweight, simple, and widely supported. However, I\'m finding that there are some

相关标签:
8条回答
  • 2021-02-19 20:42

    Have a look at http://github.com/igagis/puu/

    It is even simpler than JSON.

    It has C++ style comments.

    It is possible to format multiline strings and use escaped new line \n and tab \t chars if "real" new line or tab is needed.

    Here is the example snippet:

    "String object"
    AnotherStringObject
    "String with children"{
        "child 1"
        Child2
        "child three"{
            SubChild1
            "Subchild two"
    
            Property1 {Value1}
            "Property two" {"Value 2"}
            //comment
    
            /* multi-line
               comment */
    
            "multi-line
             string"
    
            "Escape sequences \" \n \r \t \\"
        }
    
    R"qwerty(
    This is a
    raw string, "Hello world!"
    int main(argc, argv){
        int a = 10;
        printf("Hello %d", a);
    }
    )qwerty"
    }
    
    0 讨论(0)
  • 2021-02-19 20:43

    One important attribute of JSON (probably the most important) is that you can easily "flip" between the string representation and the representation in object form, and the objects used to represent the object form are relatively simple arrays and maps. This is what makes JSON so useful in a networking context.

    The functions you want would conflict with this dual nature of JSON.

    0 讨论(0)
  • 2021-02-19 20:43

    The 'J' in JSON is "Javascript". If a particular desired syntax construct isn't in Javascript, then it won't be on JSON.

    Heredocs are beyond JSON's purview. That's a language syntax construct for simplified multi-line string definition, but JSON is a transport notation. It has nothing to do with construction. It does, however, have multiline strings, simply by allowing \n newline characters within strings. There's nothing in JSON that says you can't have a linebreak in a string. As long as the containing quote characters are correct, it's perfectly valid. e.g.

    {"x":"y\nz"}
    

    is 100% legitimate valid JSON, and is a multiline string, whereas

    {"x":"y
    z"} 
    

    isn't and will fail on parsing.

    0 讨论(0)
  • 2021-02-19 20:51

    For configuration you could use an embeddable scripting language, such as lua or python, in fact this is not an uncommon thing to do for configuration. That gives you multiline strings or here documents, and comments. It also makes it easier to have things like the boolean function you describe. However, the scripting languages are, of course, Turing complete.

    0 讨论(0)
  • 2021-02-19 20:55

    The EDN format is one option based on Clojure literals. It is almost a superset of JSON, except that no special symbol separates keys and values in maps (as : does in JSON); rather, all elements are separated by whitespace and/or a comma and a map is encoded as a list with an even number of elements, enclosed in {..}.

    EDN allows for comments (to newline using ;, or to end of the next element read using #_), but not here-docs. It is extensible to new types using a tag notation:

    #myapp/Person {:first "Fred" :last "Mertz"}
    

    The argument of the myapp/Person tag (i.e. {:first "Fred" :last "Mertz"}) must be a valid EDN expression, which makes it unextensible to here-doc support.

    It has two built-in tags: #inst for timestamps and #uuid. It also supports namespaced symbol (i.e. identifier) and keyword (i.e. map key consts) types; it distinguishes lists (..) and vectors [..]. An element of any type may be used as a key in a map.

    In the context of your above problem, one could invent an #apache/rule-or tag which accepts a sequence of elements, whose semantics I leave up to you!

    0 讨论(0)
  • 2021-02-19 20:56

    There's always what I like to call "real JSON". JSON stands for JavaScript Object Notation, and JavaScript does have comments and something close enough to heredocs.

    For the heredoc, you would use JavaScript's E4X inline XML:

    {
        longString: <>
                    Hello, world!
                    This is a long string made possible with the magic of E4X.
                    Implementing a parser isn't so difficult.
                    </>.toString() // And a comment
        /* And another
           comment */
    }
    

    You can use Firefox's JavaScript engine (FF is the only browser to support E4X currently) or you can implement your own parser, which really isn't so difficult.

    Here's the E4X quickstart guide, too.

    0 讨论(0)
提交回复
热议问题