simplejsonx

package module
v0.0.0-...-ade70a2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2025 License: MIT Imports: 3 Imported by: 0

README

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

simplejsonx

simplejsonx is a generic-based JSON parsing package that depends on github.com/bitly/go-simplejson, enhancing type safety and flexibility in JSON processing. It requires at least Go 1.22 (which supports generics).

CHINESE README

中文说明

Installation

go get github.com/giftedlunch/simplejsonx

Usage Example

1. Reading and Loading JSON Data

First, use simplejsonx.Load to read and load JSON data.

package main

import (
	"fmt"
	"github.com/giftedlunch/simplejsonx"
	"log"
)

func main() {
	// Sample JSON data
	data := []byte(`{"name": "yyle88", "age": 18, "is_rich": true}`)

	// Load the JSON data
	simpleJson, err := simplejsonx.Load(data)
	if err != nil {
		log.Fatalf("Error loading JSON: %v", err)
	}

	// Extract fields
	name, err := simplejsonx.Extract[string](simpleJson, "name")
	if err != nil {
		log.Fatalf("Error extracting 'name': %v", err)
	}

	age, err := simplejsonx.Extract[int](simpleJson, "age")
	if err != nil {
		log.Fatalf("Error extracting 'age': %v", err)
	}

	isRich, err := simplejsonx.Extract[bool](simpleJson, "is_rich")
	if err != nil {
		log.Fatalf("Error extracting 'is_rich': %v", err)
	}

	// Output the extracted values
	fmt.Println("name:", name, "age:", age, "rich:", isRich)
}
2. Extracting JSON Fields

Use Extract to extract JSON fields and ensure the correct type is returned.

res, err := simplejsonx.Extract[int](simpleJson, "age")
if err != nil {
    log.Fatalf("Error extracting 'age': %v", err)
}
fmt.Println("Age:", res)  // Output: 18
3. Resolving JSON Values

Use the Resolve method to obtain the JSON field data in the specified type.

simpleJson, err := simplejsonx.Load([]byte(`{"height": 175, "weight": 80}`))
if err != nil {
	log.Fatalf("Error loading JSON: %v", err)
}

height, err := simplejsonx.Resolve[int64](simpleJson.Get("height"))
if err != nil {
	log.Fatalf("Error resolving 'height': %v", err)
}

fmt.Println("Height:", height)  // Output: 175
4. Using Inspect to Extract Fields

The Inspect method is similar to Extract, but if the key does not exist, it returns the zero value without causing an error. It only returns an error in the case of invalid arguments or other issues. When using this method, you need to handle the returned zero value and errors correctly.

simpleJson, err := simplejsonx.Load([]byte(`{"name": "yyle88", "age": 18}`))
if err != nil {
	log.Fatalf("Error loading JSON: %v", err)
}

// Use Inspect to extract fields. If the key does not exist, it returns the zero value (e.g., empty string, zero integer).
name, err := simplejsonx.Inspect[string](simpleJson, "name")
if err != nil {
    log.Fatalf("Error inspecting 'name': %v", err)
}
fmt.Println("Name:", name)  // Output: yyle88

// Extract a non-existent field, which returns the zero value (e.g., empty string).
address, err := simplejsonx.Inspect[string](simpleJson, "address")
if err != nil {
    log.Fatalf("Error inspecting 'address': %v", err)
}
fmt.Println("Address:", address)  // Output: Empty string (zero value)

License

simplejsonx is open-source and released under the MIT License. See the LICENSE file for more information.


Support

Welcome to contribute to this project by submitting pull requests or reporting issues.

If you find this package helpful, give it a star on GitHub!

Thank you for your support!

Happy Coding with simplejsonx! 🎉

Give me stars. Thank you!!!


Starring

starring

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Extract

func Extract[T any](simpleJson *simplejson.Json, key string) (T, error)

Extract retrieves the value associated with the given key in the JSON object.

func GetList

func GetList(simpleJson *simplejson.Json, key string) (simpleJsons []*simplejson.Json, err error)

GetList retrieves a slice of simplejson.Json objects from the key's list (a[key]).

func Inspect

func Inspect[T any](simpleJson *simplejson.Json, key string) (T, error)

Inspect retrieves the value of the given key if it exists in the JSON object, returning the zero value if the key is missing.

func List

func List(elements []interface{}) (simpleJsons []*simplejson.Json)

List converts a slice of items into a slice of simplejson.Json objects.

func Load

func Load(data []byte) (simpleJson *simplejson.Json, err error)

Load creates a simplejson.Json instance from bytes, same as simplejson.NewJson(data).

func Resolve

func Resolve[T any](simpleJson *simplejson.Json) (T, error)

func Wrap

func Wrap(value interface{}) (simpleJson *simplejson.Json)

Wrap creates a simplejson.Json instance with the provided value as the root element.

Types

This section is empty.

Directories

Path Synopsis
internal
demos/main command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL