Skip to content

lakkiy/orderedjson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OrderedJSON - A Go Library for Order-Preserving JSON

Overview

This is a Go library that provides an OrderedJSON data structure which preserves the insertion order of keys during JSON marshaling and unmarshaling operations.

Important Note

⚠️ This package was created primarily to satisfy my personal needs and has limited functionality. Please review the source code before using it in your projects.

Generated by AI

This README was generated by AI and may not reflect all nuances of the implementation. Please refer to the source code for the most accurate and up-to-date information.

Features

  • Preserves JSON key insertion order during marshaling/unmarshaling
  • Simple API with New(), UnmarshalJSON(), MarshalJSON(), and Delete() methods
  • No external dependencies beyond Go standard library
  • Efficient O(1) value access through internal map structure

Installation

go get github.com/lakkiy/orderedjson

Usage

package main

import (
    "encoding/json"
    "fmt"
    "github.com/lakkiy/orderedjson"
)

func main() {
    // Create new OrderedJSON instance
    oj := orderedjson.New()
    
    // Unmarshal JSON while preserving key order
    jsonData := `{"name": "John", "age": 30, "city": "New York"}`
    err := json.Unmarshal([]byte(jsonData), oj)
    if err != nil {
        panic(err)
    }
    
    // Marshal back to JSON with preserved order
    result, err := json.Marshal(oj)
    if err != nil {
        panic(err)
    }
    
    fmt.Println(string(result))
    // Output: {"name":"John","age":30,"city":"New York"}
    
    // Delete a key
    oj.Delete("age")
    
    result, _ = json.Marshal(oj)
    fmt.Println(string(result))
    // Output: {"name":"John","city":"New York"}
}

API Reference

Types

OrderedJSON

type OrderedJSON struct {
    // Internal fields are unexported
}

Functions

New() *OrderedJSON

Creates a new empty OrderedJSON instance.

Methods

UnmarshalJSON(data []byte) error

Implements the json.Unmarshaler interface. Parses JSON data while preserving key insertion order.

MarshalJSON() ([]byte, error)

Implements the json.Marshaler interface. Serializes the OrderedJSON to JSON format maintaining key order.

Delete(key string)

Removes the specified key from the OrderedJSON structure while maintaining the order of remaining keys.

Implementation Details

  • Uses a combination of []string for key order and map[string]any for value storage
  • Token-based JSON parsing with json.NewDecoder to capture insertion order
  • Key deletion requires both map cleanup and slice manipulation

Limitations

  • Limited functionality compared to full-featured JSON libraries
  • Designed for specific use cases - review source code before adoption
  • No advanced features like nested object handling, validation, or complex data type support

License

GNU General Public License v3.0

Contributing

This project was created for personal use. If you find bugs or need additional features, please review the source code and consider forking the project for your specific needs.

About

A Go library for JSON marshaling/unmarshaling that preserves key insertion order

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages