├── .travis.yml ├── AUTHORS.md ├── LICENSE.md ├── README.md ├── reader.go └── reader_test.go /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | arch: 3 | - AMD64 4 | - ppc64le 5 | go: 6 | - 1.x 7 | - master 8 | script: 9 | go test -v -race 10 | -------------------------------------------------------------------------------- /AUTHORS.md: -------------------------------------------------------------------------------- 1 | This is the official list of JsonConfigReader authors for copyright purposes. 2 | 3 | * DisposaBoy `https://github.com/DisposaBoy` 4 | * Steven Osborn `https://github.com/steve918` 5 | * Andreas Jaekle `https://github.com/ekle` 6 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 The JsonConfigReader Authors 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/DisposaBoy/JsonConfigReader.svg?branch=master)](https://travis-ci.org/DisposaBoy/JsonConfigReader) 2 | 3 |
4 | 5 | 6 | 7 | 8 | JsonConfigReader is a proxy for [golang's io.Reader](http://golang.org/pkg/io/#Reader) that strips line comments and trailing commas, allowing you to use json as a *reasonable* config format. 9 | 10 | Comments start with `//` and continue to the end of the line. 11 | Multiline comments are also supported with `/*` and `*/`. 12 | 13 | If a trailing comma is in front of `]` or `}` it will be stripped as well. 14 | 15 | 16 | Given `settings.json` 17 | 18 | { 19 | "key": "value", // k:v 20 | 21 | // a list of numbers 22 | "list": [1, 2, 3], 23 | 24 | /* 25 | a list of numbers 26 | which are important 27 | */ 28 | "numbers": [1, 2, 3], 29 | } 30 | 31 | 32 | You can read it in as a *normal* json file: 33 | 34 | package main 35 | 36 | import ( 37 | "encoding/json" 38 | "fmt" 39 | "github.com/DisposaBoy/JsonConfigReader" 40 | "os" 41 | ) 42 | 43 | func main() { 44 | var v interface{} 45 | f, _ := os.Open("settings.json") 46 | // wrap our reader before passing it to the json decoder 47 | r := JsonConfigReader.New(f) 48 | json.NewDecoder(r).Decode(&v) 49 | fmt.Println(v) 50 | } 51 | -------------------------------------------------------------------------------- /reader.go: -------------------------------------------------------------------------------- 1 | package JsonConfigReader 2 | 3 | import ( 4 | "bytes" 5 | "io" 6 | ) 7 | 8 | type state struct { 9 | r io.Reader 10 | br *bytes.Reader 11 | } 12 | 13 | func isNL(c byte) bool { 14 | return c == '\n' || c == '\r' 15 | } 16 | 17 | func isWS(c byte) bool { 18 | return c == ' ' || c == '\t' || isNL(c) 19 | } 20 | 21 | func consumeComment(s []byte, i int) int { 22 | if i < len(s) && s[i] == '/' { 23 | s[i-1] = ' ' 24 | for ; i < len(s) && !isNL(s[i]); i += 1 { 25 | s[i] = ' ' 26 | } 27 | } 28 | if i < len(s) && s[i] == '*' { 29 | s[i-1] = ' ' 30 | s[i] = ' ' 31 | for ; i < len(s); i += 1 { 32 | if s[i] != '*' { 33 | s[i] = ' ' 34 | } else { 35 | s[i] = ' ' 36 | i++ 37 | if i < len(s) { 38 | if s[i] == '/' { 39 | s[i] = ' ' 40 | break 41 | } 42 | } 43 | } 44 | } 45 | } 46 | return i 47 | } 48 | 49 | func prep(r io.Reader) (s []byte, err error) { 50 | buf := &bytes.Buffer{} 51 | _, err = io.Copy(buf, r) 52 | s = buf.Bytes() 53 | if err != nil { 54 | return 55 | } 56 | 57 | i := 0 58 | for i < len(s) { 59 | switch s[i] { 60 | case '"': 61 | i += 1 62 | for i < len(s) { 63 | if s[i] == '"' { 64 | i += 1 65 | break 66 | } else if s[i] == '\\' { 67 | i += 1 68 | } 69 | i += 1 70 | } 71 | case '/': 72 | i = consumeComment(s, i+1) 73 | case ',': 74 | j := i 75 | for { 76 | i += 1 77 | if i >= len(s) { 78 | break 79 | } else if s[i] == '}' || s[i] == ']' { 80 | s[j] = ' ' 81 | break 82 | } else if s[i] == '/' { 83 | i = consumeComment(s, i+1) 84 | } else if !isWS(s[i]) { 85 | break 86 | } 87 | } 88 | default: 89 | i += 1 90 | } 91 | } 92 | return 93 | } 94 | 95 | // Read acts as a proxy for the underlying reader and cleans p 96 | // of comments and trailing commas preceeding ] and } 97 | // comments are delimitted by // up until the end the line 98 | func (st *state) Read(p []byte) (n int, err error) { 99 | if st.br == nil { 100 | var s []byte 101 | if s, err = prep(st.r); err != nil { 102 | return 103 | } 104 | st.br = bytes.NewReader(s) 105 | } 106 | return st.br.Read(p) 107 | } 108 | 109 | // New returns an io.Reader acting as proxy to r 110 | func New(r io.Reader) io.Reader { 111 | return &state{r: r} 112 | } 113 | -------------------------------------------------------------------------------- /reader_test.go: -------------------------------------------------------------------------------- 1 | package JsonConfigReader 2 | 3 | import ( 4 | "bytes" 5 | "io" 6 | "strings" 7 | "testing" 8 | ) 9 | 10 | var tests = map[string]string{ 11 | `{ 12 | // a 13 | "x": "y", // b 14 | "x": "y", // c 15 | }`: `{ 16 | 17 | "x": "y", 18 | "x": "y" 19 | }`, 20 | `{ 21 | /* 22 | multiline comment 23 | */ 24 | "x": "y", // b 25 | "x": "y", // c 26 | }`: `{ 27 | 28 | "x": "y", 29 | "x": "y" 30 | }`, 31 | `{ 32 | /* 33 | multiline comment with special chars in comment * * /* \* / \\ end 34 | */ 35 | "x": "y", // b 36 | "x": "y", // c 37 | }`: `{ 38 | 39 | "x": "y", 40 | "x": "y" 41 | }`, 42 | 43 | `// serve a directory 44 | "l/test": [ 45 | { 46 | "handler": "fs", 47 | "dir": "../", 48 | // "strip_prefix": "", 49 | }, 50 | ],`: ` 51 | "l/test": [ 52 | { 53 | "handler": "fs", 54 | "dir": "../" 55 | 56 | } 57 | ],`, 58 | 59 | `[1, 2, 3]`: `[1, 2, 3]`, 60 | `[1, 2, 3, 4,]`: `[1, 2, 3, 4 ]`, 61 | `{"x":1}//[1, 2, 3, 4,]`: `{"x":1} `, 62 | `//////`: ` `, 63 | `{}/ /..`: `{}/ /..`, 64 | `{,}/ /..`: `{ }/ /..`, 65 | `{,}//..`: `{ } `, 66 | `{[],}`: `{[] }`, 67 | `{[,}`: `{[ }`, 68 | `[[",",],]`: `[["," ] ]`, 69 | `[",\"",]`: `[",\"" ]`, 70 | `[",\"\\\",]`: `[",\"\\\",]`, 71 | `[",//"]`: `[",//"]`, 72 | `[]/* missing close at end`: `[] `, 73 | `[]/* missing close at end *`: `[] `, 74 | `[]/* 75 | missing close at end`: `[] `, 76 | `[",//\" 77 | "],`: `[",//\" 78 | "],`, 79 | } 80 | 81 | func TestMain(t *testing.T) { 82 | for a, b := range tests { 83 | buf := &bytes.Buffer{} 84 | io.Copy(buf, New(strings.NewReader(a))) 85 | a = buf.String() 86 | if a != b { 87 | a = strings.Replace(a, " ", ".", -1) 88 | b = strings.Replace(b, " ", ".", -1) 89 | t.Errorf("reader failed to clean json: \nexpected: `%s`, \n got `%s`", b, a) 90 | } 91 | } 92 | } 93 | --------------------------------------------------------------------------------