Code Monkey home page Code Monkey logo

orderedobject's Introduction

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.

orderedobject's People

Contributors

justhyped avatar

Stargazers

LnsTchTps avatar  avatar Peet avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.