diff --git a/x/cjson/README.md b/x/cjson/README.md new file mode 100644 index 00000000..3bb6781e --- /dev/null +++ b/x/cjson/README.md @@ -0,0 +1,31 @@ +LLGo wrapper of DaveGamble/cJSON +===== +[![Build Status](https://github.com/goplus/cjson/actions/workflows/go.yml/badge.svg)](https://github.com/goplus/cjson/actions/workflows/go.yml) +[![GitHub release](https://img.shields.io/github/v/tag/goplus/cjson.svg?label=release)](https://github.com/goplus/cjson/releases) +[![GoDoc](https://pkg.go.dev/badge/github.com/goplus/cjson.svg)](https://pkg.go.dev/github.com/goplus/cjson) +[![Compiler](https://img.shields.io/badge/compiler-llgo-darkgreen.svg)](https://github.com/goplus/llgo) +[![Language](https://img.shields.io/badge/language-Go+-blue.svg)](https://github.com/goplus/gop) + +## How to install + +```sh +mkdir build.dir +cd build.dir +cmake ../cJSON +make install +``` + +## Demos + +The `_demo` directory contains our demos (it start with `_` to prevent the `go` command from compiling it): + +* [mkjson](_demo/mkjson/mkjson.go): create a json object and print it + +### How to run demos + +To run the demos in directory `_demo`: + +```sh +cd # eg. cd _demo/mkjson +llgo run . +``` diff --git a/x/cjson/_demo/mkjson/mkjson.go b/x/cjson/_demo/mkjson/mkjson.go new file mode 100644 index 00000000..68e08649 --- /dev/null +++ b/x/cjson/_demo/mkjson/mkjson.go @@ -0,0 +1,27 @@ +package main + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/x/cjson" +) + +func main() { + mod := cjson.Object() + mod.SetItem(c.Str("name"), cjson.String(c.Str("math"))) + + syms := cjson.Array() + + fn := cjson.Object() + fn.SetItem(c.Str("name"), cjson.String(c.Str("sqrt"))) + fn.SetItem(c.Str("sig"), cjson.String(c.Str("(x, /)"))) + syms.AddItem(fn) + + v := cjson.Object() + v.SetItem(c.Str("name"), cjson.String(c.Str("pi"))) + syms.AddItem(v) + + mod.SetItem(c.Str("items"), syms) + + c.Printf(c.Str("%s\n"), mod.CStr()) + mod.Delete() +} diff --git a/x/cjson/cjson.go b/x/cjson/cjson.go new file mode 100644 index 00000000..e91e86c9 --- /dev/null +++ b/x/cjson/cjson.go @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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).AddItem C.cJSON_AddItemToArray +func (o *JSON) AddItem(item *JSON) c.Int { return 0 } + +// Append item to the specified object. +// +// llgo:link (*JSON).SetItem 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 } diff --git a/x/cjson/llgo_autogen.lla b/x/cjson/llgo_autogen.lla new file mode 100644 index 00000000..a5c17d01 Binary files /dev/null and b/x/cjson/llgo_autogen.lla differ