Giter Site home page Giter Site logo

fastjson's Introduction

fastjson

10x faster then standard lib & dynamic set json

examples

go get github.com/daqnext/fastjson

import 
(
    FastJson "github.com/daqnext/fastjson"
)
//////////////////////////////////////////////////////////////////

fj, err := FastJson.NewFromString("{\"test\":123}")
result, _ := fj.GetInt("test")
if result != 123 {
	t.Error("read error")
}
//////////////////////////////////////////////////////////////////

fj := FastJson.NewFromString("{\"test\":123}")
result := fj.GetContentAsString()
if result != "{\"test\":123}" {
	t.Error("read error")
}

//////////////////////////////////////////////////////////////////

fj, err := FastJson.NewFromFile("../test.json")
if err == nil {
	result, _ := fj.GetString("company", "name")
	if result == "acm" {
		t.Error("read error")
	}

}

//////////////////////////////////////////////////////////////////

fj, err := FastJson.NewFromFile("../test.json")
if err == nil {

	result, _ := fj.GetString("company", "name")
	if result == "acm" {
		t.Error("read error")
	}

	_, err := fj.GetBoolean("company", "name")
	if err == nil {
		t.Error("get boolean error")
	}

	flnum, _ := fj.GetInt64("person", "github", "followers")
	if flnum != 109 {
		t.Error("read int error")
	}
}

//////////////////////////////////////////////////////////////////


fj, err := FastJson.NewFromFile("../test.json")
if err == nil {
	fj.ArrayEach(func(value []byte, offset int, err error) {
		link, _ := FastJson.GetString(value, "url")
		if link != "link1" {
			t.Error("read nested error")
		}
	}, "person", "avatars")
}

//////////////////////////////////////////////////////////////////


fj, err := FastJson.NewFromFile("../test.json")
if err == nil {
	link, _ := fj.GetString("person", "avatars", "[0]", "url")
	if link != "link1" {
		t.Error("read  array error")
	}
}

//////////////////////////////////////////////////////////////////

fj, err := FastJson.NewFromFile("../test.json")
if err == nil {
	fj.ObjectEach(func(key []byte, value []byte, offset int) error {
		if string(key) == "last" {
			if string(value) != "Bugaev" {
				t.Error("read object error")
			}
		}
		return nil
	}, "person", "name")
}

//////////////////////////////////////////////////////////////////


fj, err := FastJson.NewFromFile("../test.json")
if err == nil {
	last, _ := fj.GetString("person", "name", "last")
	if last != "Bugaev" {
		t.Error("read  object item error")
	}
}

//////////////////////////////////////////////////////////////////

fj, err := FastJson.NewFromFile("../test.json")
if err == nil {
	fj.SetFloat64(123.123, "person", "weight")
	fj.ClearFileAndOutput("../test.json")
}

fj2, err2 := FastJson.NewFromFile("../test.json")
if err2 == nil {
	r, _ := fj2.GetFloat64("person", "weight")
	if r != 123.123 {
		t.Error("read or set float error")
	}
}

//////////////////////////////////////////////////////////////////

fastjson's People

Contributors

leolikescoding avatar

Watchers

 avatar Sherlock avatar  avatar

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.