Coder Social home page Coder Social logo

fnugk / json2struct Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marhaupe/json2struct

0.0 0.0 0.0 1.38 MB

CLI tool to convert JSON to struct type definitions

License: GNU General Public License v3.0

Ruby 3.74% Go 95.07% Makefile 1.20%

json2struct's Introduction

json2struct

Project Status: Active โ€“ The project has reached a stable, usable state and is being actively developed. Build Status codecov PRs Welcome

CLI tool to convert JSON to struct type definitions

At some point when dealing with JSONs in Go, you will have to write struct types to json.Unmarshal your JSONs into. Doing this by hand is not only repetitive and time consuming, but also error prone. json2struct saves you this work by automatically parsing the JSON and generating you the matching struct type definitions ready to be used.

Unlike other tools, json2struct tries to avoid generating interface{} and map[string]interface{} as much as possible. Nonetheless it's very fast ๐Ÿš€.

Installation

Homebrew

brew tap marhaupe/json2struct https://github.com/marhaupe/json2struct

brew install marhaupe/json2struct/json2struct

Manually

Grab the latest release binaries.

Usage

json2struct [options]

Calling json2struct without flags opens a text editor. Simply input your JSON and save and exit.

Example

Options

You probably don't want to manually write that 1MB JSON you have to generate a struct for by hand. I mean, if you really want to, I'm not here to judge, but that's not the point. These options will make your life easier. If you miss some, feel free to open an issue.

Generating a struct from a string

-s, --string string: JSON string

This is basically your bread and butter thanks to pipes. Usage:

 json2struct -s "$(curl "https://reqres.in/api/users?page=2")"

Generating a struct from an existing file

-f, --file string: path to JSON file

This is useful if you have a JSON file stored in your filesystem and are too lazy to use pipes. Usage:

json2struct -f input.json

Generating a struct from the clipboard to the clipboard

-c, --clipboard: read from and write types to clipboard

Reads JSON from clipboard, generates types and writes those types to the clipboard.

json2struct -c

Other options

-b, --benchmark: measure execution time

-h, --help: help for json2struct

--version: version for json2struct

Specifics

A lot of the lexing/parsing in this project is inspired by the text/template package. Rob Pike also gave a talk about how and why they wrote the package. The link can be found here, and the slides to it here

json2struct's People

Contributors

marhaupe avatar cordobo 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.