Git Product home page Git Product logo

hjson-go's Introduction

NPM Maven Central PyPI nuget Packagist crate crate Go Pkg LuaRocks

Hjson, a user interface for JSON

Hjson

Adds comments, makes it nicer to read and avoids comma mistakes.

For details see hjson.github.io.

hjson-go's People

Contributors

0xflotus avatar alpancs avatar antonoks avatar bronze1man avatar dimiro1 avatar dqsully avatar gabrielnicolasavellaneda avatar hmalphettes avatar jcgruenhage avatar laktak avatar leoriether avatar mspiegel avatar neilalexander avatar trobro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hjson-go's Issues

Includes trailing comma character in unquoted list items

Given the following hjson:

{
    foo: [
        bar,
        baz
    ]
}

I would expect the output to be:

{
  "foo": [
    "bar",
    "baz"
  ]
}

But instead I get:

{
  "foo": [
    "bar,",
    "baz"
  ]
}

To get the expected output, I need to quote the list item or not use a trailing comma in lists.

Integers are unmarshalled into float64

Current implementation decode integers into float64 values.
This introduces a limitation in the integer values magnitude that can be handled by the hjson library.

newline

IMG_20220101_115101
How to newLine on hexDump field not all filed new line fmt?

Support comments marshaling

It would be very useful to be able to marshal (encode) a struct and embed comments in the resulting hjson.

Preserve comments on source file

I was wondering if there is a way to Unmarshal hjson to a go struct but preserve arbitrary comments on the source json. For example if my hjson looks like this:

{
// random comment
  "foo": "bar",
  "foo2": "bar2"
}

I want to preserve a comment on a random line in the json, unmarshal the file to a go struct, change bar to bar-modified.
Then marshal and save to file.

Is it possible to preserve comments when you don't know which line might have a comment on it?

"Unmarshal to structs" example in readme doesn't work

The structs are left as their zero values. The errors are ignored in the example code. If I log the errors, it says reflect.Set: value of type map[string]interface {} is not assignable to type main.Sample.

my Go version: go1.20.7 linux/amd64

can not unmarshal obj from marshal result with string "0\r'"

test code:

func TestEncodeMarshalAndUnmarshal(t *testing.T) {
	type t_obj struct{
		F string
	}
	const testS = "0\r'"
	input := t_obj{
		F: "0\r'",
	}
	buf, err := Marshal(input)
	if err != nil {
		t.Fatal(err)
	}
	var obj2 t_obj
	err = Unmarshal(buf,&obj2)
	if err != nil {
		t.Fatal(err)
	}
	if obj2.F!=testS{
		t.Error("fail ["+hex.Dump([]byte(obj2.F))+`]`)
	}
}

Trailing spaces in Marshal output

When producing hjson out of a struct with in a struct that has comments on the fields, there's a bunch of additional trailing spaces in there. See the attached picture for an example, which I created with this piece of code:

package main

import (
	"github.com/hjson/hjson-go"
	"fmt"
)

type Config struct {
	InnerConfig	InnerConfig	`comment:"This is a\ncomment with some\nnewlines in between."`
}

type InnerConfig struct {
	ValueOne	string	`comment:"This is a\ncomment with some\nnewlines in between."`
	ValueTwo	bool	`comment:"This is a\ncomment with some\nnewlines in between."`
}

func main() {
	cfg := Config{}
	cfg.InnerConfig = InnerConfig{}
	cfg.InnerConfig.ValueOne = "test"
	cfg.InnerConfig.ValueTwo = true

	hjson, _ := hjson.Marshal(cfg)

	fmt.Println(string(hjson))
}

image

Docs: null behavior with *string

Minor documentation inconsistency?

Readme:

String pointer destinations are treated the same as string destinations, so you cannot set a string pointer to nil by writing null in an Hjson file. Writing null in an Hjson file would result in the string "null" being stored in the destination string pointer.

4.4.0 Release notes

Slightly changed behavior for unmarshalling "null" into a string destination: if the value is "null" without quotes and the destination is a pointer then the pointer is set to nil instead of a pointer to the string "null", even if the destination is a pointer to a string or a type that implements UnmarshalText().

CLI gets stuck when no file is used as parameter

When using hjson -j without a file, hjson gets stuck.
An exit code 1 would be preferable then.
I stumbled across this, when I wanted to write a systemctl system service that parses a hjson file, but the environment variable was not set (as it the defualt in system services) and then the script was stuck there forever. Only after inspecting the tree with htop I could see that hjson is executed forever.

need an option to preserve source file key order

i'm trying to remain compatible with a python implementation of the hjson format, and need the keys to remain in order.

this is preventing me from being able to utilize this tool due to it's key sorting in the output.

i understand that in go, a map is not guaranteed an order, but the keys could be imported into a list on read and exported back out in the same order that they appear, i.e. to simulate a python 'ordered dictionary.

this doesn't need to be the default behaviour since often the sorting is desired, but as a compatibility option or flag to 'preserve key order' would be ideal.

thank you for considering.

Error with UnmashalText type with null value in list

The Unmarshal function will always return an error when it tries to parse a list of object, whose internal property type is an pointer which implements UnmarshalText interface and the value is null.

type Foo struct {
	Bar *Bar
}

type Bar struct {
}

func (b *Bar) UnmarshalText(text []byte) error {
	return nil
}

func TestHjson_NullPropertyUnmarshalTextObjectInList(t *testing.T) {
	s := []byte("[{\"Bar\":null}]")

	foos := []*Foo{}
	err := hjson.Unmarshal(s, &foos)

	assert.ErrorContains(t, err, "End of input while parsing an object (did you forget a closing '}'?)")
}

homebrew?

maybe you can add a homebrew formula?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.