Code Monkey home page Code Monkey logo

jsonpreparator's Introduction

JsonPreparator

Russian Readme

Russian Readme is here

About

Service for the preparation of JSON according to the proposed scheme. Adds missing elements, removes unnecessary items.

Install

composer require kaizer666/json-preparator

Usage

use Json\JsonPreparator;

function test() {
    $require = true;
    $schema = "/path/to/schema.json";
    $data = json_decode('{
      "integer_with_def": 2.0002,
      "float": 123000,
      "string": "text",
      "object": {},
      "array_string": [
      "1.1.1.1.1.1.",
      "1"
      ],
      "array_integer": [
      "1.1.1.1.1.",
      "2.2.2.2."
      ],
      "array": [
        {
          "integer_with_def": 1,
          "float": 1000,
          "text": "text1"
        },{
          "integer_with_def": 2,
          "float": 2000,
          "text": "text2"
        },{
          "integer_with_def": 3,
          "float": 3000,
          "text": "text3"
        },{
          "integer_with_def": 4,
          "float": 4000,
          "text": "text4"
        }
      ]
    }', true);
                
    $preparator = new JsonPreparator($schema, $require);
    $output = $preparator->prepare($data);
    return response()->json($output);
}

Schema example

{
  "integer_with_def": {
	"type": "integer",
	"default": "32",
	"require": false
  },
  "integer": {
	"type": "integer"
  },
  "float": {
	"type": "float",
	"format": {
	  "decimals":2,
	  "dec_point":".",
	  "thousands_sep":" "
	},
    "require": false
  },
  "string": {
	"type":"string"
  },
  "object": {
	"type":"object",
	"components": {
	  "integer_with_def": {
		"type": "integer",
		"default": "32",
        "require": false
	  },
	  "integer": {
		"type": "integer"
	  },
	  "float": {
		"type": "float",
		"format": {
		  "decimals":2,
		  "dec_point":".",
		  "thousands_sep":" "
		}
	  }
	}
  },
  "array_string":{
	"type": "array",
	"values": {
	  "type": "string",
      "require": false
	}
  },
  "array_integer":{
	"type": "array",
	"values": {
	  "type": "integer"
	}
  },
  "array": {
	"type": "array",
	"values": {
	  "type": "object",
	  "components": {
		"integer_with_def": {
		  "type": "integer",
		  "default": "32"
		},
		"integer": {
		  "type": "integer"
		},
		"float": {
		  "type": "float",
		  "format": {
			"decimals":2,
			"dec_point":".",
			"thousands_sep":" "
		  }
		},
		"string": {
		  "type":"string"
		},
		"array_string":{
		  "type": "array",
		  "values": {
			"type": "string"
		  }
		},
		"array_integer":{
		  "type": "array",
		  "values": {
			"type": "integer"
		  }
		}
	  }
	}
  }
}

Field Types

1. "integer"
    - Integer number
    - Options:
        a) default - the default value if there is no element in the incoming array
        b) require - a required value
2. "float"
    - A float number
    - Options:
        a) default - the default value if there is no element in the incoming array
        b) format - number format:
            - decimals - how many decimal places
            - dec_point - decimal separator
            - thousands_sep - thousands separator
        c) require - a required value
3. "string"
    - String
    - Options:
        a) default - the default value if there is no element in the incoming array
        b) require - a required value
4. "object"
    - Object {} (associative array)
    - Options:
        a) components - the list of components of the object
        b) require - a required value
5. "array"
    - Array []
    - Options:
        a) values ​​- list of array values
        b) require - a required value

jsonpreparator's People

Watchers

James Cloos 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.