Skip to content

A map that respects insertion order when serialized to JSON

License

Notifications You must be signed in to change notification settings

Hyper-Solutions/orderedobject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

orderedobject

Go Reference

orderedobject is a simple Go package that provides an Object type for representing JSON objects that respect insertion order. It uses generics to allow for type-safe usage with any value type.

Installation

To install the package, use go get:

go get github.com/Hyper-Solutions/orderedobject

Usage

Here's a simple usage example:

package main

import (
    "encoding/json"
    "fmt"

    "github.com/Hyper-Solutions/orderedobject"
)

func main() {
    // Create a new object with a capacity of 3
    obj := orderedobject.NewObject[any](3)

    // Set key-value pairs
    obj.Set("name", "John")
    obj.Set("age", 30)
    obj.Set("city", "New York")

    // Marshal the object to JSON
    encoded, err := json.Marshal(obj)
    if err != nil {
        panic(err)
    }

    fmt.Println(string(encoded))
    // Output: {"name":"John","age":30,"city":"New York"}
}

The Object type provides the following methods:

  • NewObject[V any](capacity int) *Object[V]: Creates a new Object with the specified capacity.
  • Set(key string, value V): Sets a key-value pair in the object. If the key already exists, its value is replaced.
  • Has(key string) bool: Checks if a key is set in the object.
  • Get(key string) V: Retrieves the value associated with a key. If the key is not set, the zero value of type V is returned.
  • MarshalJSON() ([]byte, error): Marshals the object to JSON, respecting the insertion order of key-value pairs.

License

This package is licensed under the MIT License.

About

A map that respects insertion order when serialized to JSON

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages