Initial working version.

This commit is contained in:
2026-09-29 19:10:32 +05:30
parent 4bd3cd23e6
commit 5cd2b38c9c
4 changed files with 1207 additions and 0 deletions
+257
View File
@@ -0,0 +1,257 @@
package jaeson
import (
"errors"
"strconv"
"strings"
)
type JSONArray struct {
sarr []string
oarr []*JSONObject
aarr []*JSONArray
narr []int
blnarr []bool
flt64arr []float64
ctsarr int
ctoarr int
ctaarr int
ctnarr int
ctblnarr int
ctflt64arr int
hasVal bool
}
func (v *JSONArray) Init() {
v.ctsarr = 0
v.ctoarr = 0
v.ctaarr = 0
v.ctnarr = 0
v.ctblnarr = 0
v.ctflt64arr = 0
v.sarr = make([]string, 0)
v.oarr = make([]*JSONObject, 0)
v.aarr = make([]*JSONArray, 0)
v.narr = make([]int, 0)
v.blnarr = make([]bool, 0)
v.flt64arr = make([]float64, 0)
v.hasVal = false
}
func (v *JSONArray) Size() int {
return v.ctsarr + v.ctoarr + v.ctaarr + v.ctnarr
}
//---------- ADD ----------//
func (v *JSONArray) AddString(value string) error {
v.sarr = append(v.sarr, value)
v.ctsarr += 1
v.hasVal = true
return nil
}
func (v *JSONArray) AddJSONObject(value *JSONObject) error {
v.oarr = append(v.oarr, value)
v.ctoarr += 1
v.hasVal = true
return nil
}
func (v *JSONArray) AddJSONArray(value *JSONArray) error {
v.aarr = append(v.aarr, value)
v.ctaarr += 1
v.hasVal = true
return nil
}
func (v *JSONArray) AddInt(value int) error {
v.narr = append(v.narr, value)
v.ctnarr += 1
v.hasVal = true
return nil
}
func (v *JSONArray) AddBool(value bool) error {
v.blnarr = append(v.blnarr, value)
v.ctblnarr += 1
v.hasVal = true
return nil
}
func (v *JSONArray) AddFloat(value float64) error {
v.flt64arr = append(v.flt64arr, value)
v.ctflt64arr += 1
v.hasVal = true
return nil
}
//---------- PUT ----------//
func (v *JSONArray) PutJSONObject(value *JSONObject) error {
return v.AddJSONObject(value)
}
func (v *JSONArray) PutString(value string) error {
return v.AddString(value)
}
func (v *JSONArray) PutJSONArray(value *JSONArray) error {
return v.AddJSONArray(value)
}
func (v *JSONArray) PutInt(value int) error {
return v.AddInt(value)
}
func (v *JSONArray) PutBool(value bool) error {
return v.AddBool(value)
}
func (v *JSONArray) PutFloat(value float64) error {
return v.AddFloat(value)
}
//---------- GET ----------//
func (v *JSONArray) GethasVal() bool {
return v.hasVal
}
func (v *JSONArray) GetString(index int) string {
return v.sarr[index]
}
func (v *JSONArray) GetStringArray() []string {
return v.sarr
}
func (v *JSONArray) FromStringArray(pArr []string) {
v.sarr = pArr
}
func (v *JSONArray) GetJSONObject(index int) (*JSONObject, error) {
if index > -1 {
if v.ctoarr > 0 {
return v.oarr[index], nil
} else {
return nil, errors.New("index out of range: " + strconv.Itoa(index))
}
} else {
return nil, errors.New("index out of range: " + strconv.Itoa(index))
}
}
func (v *JSONArray) GetJSONArray(index int) (*JSONArray, error) {
if index > -1 {
if v.ctaarr > 0 {
return v.aarr[index], nil
} else {
return nil, errors.New("index out of range: " + strconv.Itoa(index))
}
} else {
return nil, errors.New("index out of range: " + strconv.Itoa(index))
}
}
func (v *JSONArray) GetInt(index int) int {
return v.narr[index]
}
func (v *JSONArray) GetNumber(index int) int {
return v.narr[index]
}
func (v *JSONArray) GetBool(index int) bool {
return v.blnarr[index]
}
func (v *JSONArray) GetFloat(index int) float64 {
return v.flt64arr[index]
}
func (v *JSONArray) RemoveLastElem() {
if v.ctaarr > 0 {
v.aarr = v.aarr[:len(v.aarr)-1]
v.ctaarr -= 1
}else if v.ctoarr > 0 {
v.oarr = v.oarr[:len(v.oarr)-1]
v.ctoarr -= 1
}else if v.ctsarr > 0 {
v.sarr = v.sarr[:len(v.sarr)-1]
v.ctsarr -= 1
}else if v.ctnarr > 0 {
v.narr = v.narr[:len(v.narr)-1]
v.ctnarr -= 1
}
}
//---------- PRINT STRING----------//
func (v *JSONArray) String() string {
var out string = "["
if v.ctsarr > 0 {
for idx := range v.sarr {
out += "\"" + v.sarr[idx] + "\","
}
}
if v.ctoarr > 0 {
for idx, _ := range v.oarr {
jsObj := v.oarr[idx]
out += jsObj.String() + ","
}
}
if v.ctaarr > 0 {
for idx, _ := range v.aarr {
jsArr := v.aarr[idx]
out += jsArr.String() + ","
}
}
if v.ctnarr > 0 {
for idx, _ := range v.narr {
out += strconv.Itoa(v.narr[idx]) + ","
}
}
if v.ctblnarr > 0 {
for idx, _ := range v.blnarr {
out += strconv.FormatBool(v.blnarr[idx]) + ","
}
}
if v.ctflt64arr > 0 {
for idx, _ := range v.flt64arr {
out += strconv.FormatFloat(v.flt64arr[idx], 'f', -1, 64 ) + ","
}
}
if strings.HasSuffix(out, ",") {
out = out[0 : len(out)-1]
}
out += "]"
return out
}
//---------- PRINT STRING----------//
// JSON Canonicalisation Scheme (JCS)
func (v *JSONArray) StringJCS() string {
var out string = "["
// TO BE IMPLEMENTED
out += "]"
return out
}