Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
cjson.go
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed May 14, 2024
1 parent 2c73942 commit af5087f
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
13 changes: 13 additions & 0 deletions _demo/mkjson/mkjson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"github.com/goplus/cjson"
"github.com/goplus/llgo/c"
)

func main() {
mod := cjson.Object()
mod.SetItem(c.Str("name"), cjson.String(c.Str("math")))
c.Printf(c.Str("%s\n"), mod.CStr())
mod.Delete()
}
99 changes: 99 additions & 0 deletions cjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,102 @@
*/

package cjson

import (
_ "unsafe"

"github.com/goplus/llgo/c"
)

const (
LLGoPackage = "link: cjson"
)

// llgo:type C
type JSON struct {
Unused [0]byte
}

//go:linkname Null C.cJSON_CreateNull
func Null() *JSON

//go:linkname True C.cJSON_CreateTrue
func True() *JSON

//go:linkname False C.cJSON_CreateFalse
func False() *JSON

//go:linkname Bool C.cJSON_CreateBool
func Bool(boolean c.Int) *JSON

//go:linkname Number C.cJSON_CreateNumber
func Number(num float64) *JSON

//go:linkname String C.cJSON_CreateString
func String(str *c.Char) *JSON

//go:linkname Array C.cJSON_CreateArray
func Array() *JSON

//go:linkname Object C.cJSON_CreateObject
func Object() *JSON

// raw json
//
//go:linkname Raw C.cJSON_CreateRaw
func Raw(raw *c.Char) *JSON

// Create a string where valuestring references a string so
// it will not be freed by Delete
//
//go:linkname StringRef C.cJSON_CreateStringReference
func StringRef(str *c.Char) *JSON

// Create an object that only references it's elements so
// they will not be freed by Delete
//
//go:linkname ObjectRef C.cJSON_CreateObjectReference
func ObjectRef(child *JSON) *JSON

// Create an array that only references it's elements so
// they will not be freed by Delete
//
//go:linkname ArrayRef C.cJSON_CreateArrayReference
func ArrayRef(child *JSON) *JSON

// Delete a JSON entity and all subentities.
//
// llgo:link (*JSON).Delete C.cJSON_Delete
func (o *JSON) Delete() {}

// Append item to the specified array.
//
// llgo:link (*JSON).Add C.cJSON_AddItemToArray
func (o *JSON) AddItem(item *JSON) c.Int { return 0 }

// Append item to the specified object.
//
// llgo:link (*JSON).Set C.cJSON_AddItemToObject
func (o *JSON) SetItem(key *c.Char, item *JSON) c.Int { return 0 }

// llgo:link (*JSON).CStr C.cJSON_PrintUnformatted
func (o *JSON) CStr() *c.Char { return nil }

// Render a JSON entity to text for transfer/storage.
//
// llgo:link (*JSON).Print C.cJSON_Print
func (o *JSON) Print() *c.Char { return nil }

// Render a JSON entity to text for transfer/storage without any formatting.
//
// llgo:link (*JSON).PrintUnformatted C.cJSON_PrintUnformatted
func (o *JSON) PrintUnformatted() *c.Char { return nil }

// Render a JSON entity to text using a buffered strategy.
//
// prebuffer is a guess at the final size. guessing well reduces reallocation.
//
// fmt=0 gives unformatted, =1 gives formatted.
//
// llgo:link (*JSON).PrintBuffered C.cJSON_PrintBuffered
func (o *JSON) PrintBuffered(prebuffer c.Int, fmt c.Int) *c.Char { return nil }
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/goplus/cjson

go 1.18

require github.com/goplus/llgo v0.8.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/goplus/llgo v0.8.1 h1:M02K0unq0Ee/uK5wW1EBPCFJ+LxfQq45kkJawS5maRE=
github.com/goplus/llgo v0.8.1/go.mod h1:FZVAPoRTpAuQIizMP7wcZYcZ25Ft+GUX6WhD23GcFhc=

0 comments on commit af5087f

Please sign in to comment.