Code Monkey home page Code Monkey logo

json's Introduction

json

!WARNING! This repository moved and maintained here: https://github.com/infiniteloopcloud/json

Copy of Golang's json library with IsZero feature from CL13977

Disclaimer

It is a package primary used for my own projects, I will keep it up-to-date with the mainline versioning of Go.

Usage

Exactly the same as Go's JSON. Plus...

package main

import (
	"encoding/json"
	"fmt"
)

type nullString struct {
	Valid  bool
	String string
}

func (n nullString) IsZero() bool {
	return !n.Valid
}

type RandomDataSet struct {
	Data1 nullString `json:"data1,omitempty"`
	Data2 nullString `json:"data2,omitempty"`
}

func main() {
	result, err := json.Marshal(RandomDataSet{Data1: nullString{Valid: true, String: "test"}})
	if err != nil {
		panic(err)
	}
	fmt.Println(string(result))
}

// Output: {"data1":{"Valid":true,"String":"test"}}

json's People

Contributors

pumpkinseed avatar

Stargazers

Wolfy avatar Márk Zsibók (D3v) avatar Robert Boros avatar Hajnal Andor avatar

Watchers

James Cloos avatar  avatar  avatar Adam Chalkley avatar

json's Issues

PodTemplateSpec in k8s print "null" values for keys

package main

import (
  "fmt"
  "github.com/PumpkinSeed/json"
  v1 "k8s.io/api/core/v1"
)
func main() {
  spec := v1.PodTemplateSpec{
    Spec: v1.PodSpec{
      Containers: []v1.Container{
        {
          Name: "sample",
        },
      },
    },
  }
  out, _ := json.Marshal(spec)
  fmt.Println(string(out))
}

Output:

{"metadata":{"creationTimestamp":null},"spec":{"containers":[{"name":"sample","resources":{}}]}}

How do I resolve this to not print "null" valued keys?

Ref: PodTemplateSpec

Does this work only if the struct implements IsZero() function?

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.