Download the PHP package dcarbone/json-to-go without Composer
On this page you can find all versions of the php package dcarbone/json-to-go. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dcarbone/json-to-go
More information about dcarbone/json-to-go
Files in dcarbone/json-to-go
Package json-to-go
Short Description PHP Implementation of mholt/json-to-go
License MIT
Informations about the package json-to-go
json-to-go
PHP Implementation of mholt/json-to-go
Composer
Why Do This in PHP?
Because it fits better into my personal workflow.
Also because why not.
Basic Usage
Once included in your project, the easiest way to use it is probably using the static initializers:
This will return to you an instance of a concrete implementation of AbstractType with your input parsed. If there was an issue during parsing, an exception will be thrown.
For a complete list of possible types, see here
This class implements __toString()
, and the return value is the parsed GO object.
General Rules
- If a property name in an object is entirely comprised of numbers, it will be prefixed with
Num
- If a property name in an object begins with a non-alphanumeric value, it will be prefixed with
X
- If a property name in an object has a numerical first character, that character will be converted to a string
following the map seen here. (e.g.:
80211X
becomesEight_0211X
) You may optionally define your own map when initializing the Configuration class. - If a type is not possible (is a NULL in the example json...), or if there is a value type conflict between keys
within a map, the type will be defined as
interface{}
- The names for things will always be Exported
- It is always a good idea to run the results through
go fmt
Custom Configuration
There are a few possible options when parsing JSON, definable in the Configuration class:
forceOmitEmpty
- Will always place thejson:,omitempty
markup at the end of struct propertiesforceIntToFloat
- Will convert allint
types tofloat
.useSimpleInt
- Will, if using ints, use the simpleint
type rather than attempting to determineint32
vsint64
forceScalarToPointer
- Will turn all simple types (string, int, float, bool) into pointersemptyStructToInterface
- Will convert an object without properties into aninterface{}
breakOutInlineStructs
- Will create bespoke type definitions for nested objectssanitizeInput
- Will override the values present in the example json (see here)initialNumberMap
- Array to use for converting number characters to alpha characters at the beginning of structcallbacks
- Instance of Callbacks or array of['callback' => callable]
wherecallback
== name of parameter on Callbacks andcallable
== any valid php callable that accepts the parameters present in the function named the same as the property.
Map and RawMessage types
By default, the RawMessageType
are never used. To take advantage of these types, you must implement your own goType
that returns either map
or raw
respectively.
Examples:
Taking this example JSON payload (source):
And executing the following:
Will result in (pre go fmt
):