{"id":18884229,"url":"https://github.com/dcarbone/json-to-go","last_synced_at":"2026-04-11T08:05:31.599Z","repository":{"id":56964021,"uuid":"72135905","full_name":"dcarbone/json-to-go","owner":"dcarbone","description":"PHP Implementation of mholt/json-to-go","archived":false,"fork":false,"pushed_at":"2017-08-03T19:11:09.000Z","size":86,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-31T04:22:41.512Z","etag":null,"topics":["golang","json","parsing","php"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dcarbone.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-27T18:20:29.000Z","updated_at":"2019-07-21T08:05:22.000Z","dependencies_parsed_at":"2022-08-21T05:40:27.315Z","dependency_job_id":null,"html_url":"https://github.com/dcarbone/json-to-go","commit_stats":null,"previous_names":[],"tags_count":43,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcarbone%2Fjson-to-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcarbone%2Fjson-to-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcarbone%2Fjson-to-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcarbone%2Fjson-to-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dcarbone","download_url":"https://codeload.github.com/dcarbone/json-to-go/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239854311,"owners_count":19708051,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","json","parsing","php"],"created_at":"2024-11-08T07:11:42.932Z","updated_at":"2026-04-11T08:05:31.544Z","avatar_url":"https://github.com/dcarbone.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json-to-go\nPHP Implementation of mholt/json-to-go\n\n## Composer\n\n```json\n{\n    \"require\": {\n        \"dcarbone/json-to-go\": \"@stable\"\n    }\n}\n```\n\n## Why Do This in PHP?\n\nBecause it fits better into my personal workflow.\n\nAlso because why not.\n\n## Basic Usage\n\nOnce included in your project, the easiest way to use it is probably using the static initializers:\n\n```php\n$jsonToGO = \\DCarbone\\JSONToGO::parse('RootTypeName', $myjson);\n```\n\nThis will return to you an instance of a concrete implementation of\n[AbstractType](./src/JSONToGO/Types/AbstractType.php) with your input parsed.  If there was an issue during parsing,\nan exception will be thrown.\n\nFor a complete list of possible types, see [here](./src/JSONToGO/Types)\n\nThis class implements `__toString()`, and the return value is the parsed GO object.\n\n## General Rules\n\n- If a property name in an object is entirely comprised of numbers, it will be prefixed with `Num`\n- If a property name in an object begins with a non-alphanumeric value, it will be prefixed with `X`\n- If a property name in an object has a numerical first character, that character will be converted to a string\n  following the map seen [here](./src/JSONToGO/Configuration.php#L25). (e.g.: `80211X` becomes `Eight_0211X`)\n  You may optionally define your own map when initializing the Configuration class. \n- If a type is not possible (is a NULL in the example json...), or if there is a value type conflict between keys\n  within a map, the type will be defined as `interface{}`\n- The names for things will always be Exported\n- It is always a good idea to run the results through `go fmt`\n\n## Custom Configuration\n\nThere are a few possible options when parsing JSON, definable in the [Configuration](./src/JSONToGO/Configuration.php)\nclass:\n\n- `forceOmitEmpty` - Will always place the `json:,omitempty` markup at the end of struct properties\n- `forceIntToFloat` - Will convert all `int` types to `float`.\n- `useSimpleInt` - Will, if using ints, use the simple `int` type rather than attempting to determine `int32` vs `int64`\n- `forceScalarToPointer` - Will turn all simple types (string, int, float, bool) into pointers\n- `emptyStructToInterface` - Will convert an object without properties into an `interface{}`\n- `breakOutInlineStructs` - Will create bespoke type definitions for nested objects\n- `sanitizeInput` - Will override the values present in the example json (see [here](./src/JSONToGO.php#L121))\n- `initialNumberMap`- Array to use for converting number characters to alpha characters at the beginning of struct\n- `callbacks` - Instance of [Callbacks](./src/JSONToGO/Callbacks.php) or array of `['callback' =\u003e callable]` where\n  `callback` == name of parameter on [Callbacks](./src/JSONToGO/Callbacks.php) and `callable` == any valid php callable\n  that accepts the parameters present in the function named the same as the property.\n\n### Map and RawMessage types\n\nBy default, the [MapType](src/JSONToGO/Types/MapType.php) and [RawMessageType](./src/JSONToGO/Types/RawMessageType.php)\nare never used.  To take advantage of these types, you must implement your own [goType](./src/JSONToGO/Typer.php#L30)\nthat returns either `map` or `raw` respectively.\n\n## Examples:\n\nTaking this example JSON payload ([source](http://json.org/example.html)):\n\n```json\n{\"web-app\": {\n  \"servlet\": [   \n    {\n      \"servlet-name\": \"cofaxCDS\",\n      \"servlet-class\": \"org.cofax.cds.CDSServlet\",\n      \"init-param\": {\n        \"configGlossary:installationAt\": \"Philadelphia, PA\",\n        \"configGlossary:adminEmail\": \"ksm@pobox.com\",\n        \"configGlossary:poweredBy\": \"Cofax\",\n        \"configGlossary:poweredByIcon\": \"/images/cofax.gif\",\n        \"configGlossary:staticPath\": \"/content/static\",\n        \"templateProcessorClass\": \"org.cofax.WysiwygTemplate\",\n        \"templateLoaderClass\": \"org.cofax.FilesTemplateLoader\",\n        \"templatePath\": \"templates\",\n        \"templateOverridePath\": \"\",\n        \"defaultListTemplate\": \"listTemplate.htm\",\n        \"defaultFileTemplate\": \"articleTemplate.htm\",\n        \"useJSP\": false,\n        \"jspListTemplate\": \"listTemplate.jsp\",\n        \"jspFileTemplate\": \"articleTemplate.jsp\",\n        \"cachePackageTagsTrack\": 200,\n        \"cachePackageTagsStore\": 200,\n        \"cachePackageTagsRefresh\": 60,\n        \"cacheTemplatesTrack\": 100,\n        \"cacheTemplatesStore\": 50,\n        \"cacheTemplatesRefresh\": 15,\n        \"cachePagesTrack\": 200,\n        \"cachePagesStore\": 100,\n        \"cachePagesRefresh\": 10,\n        \"cachePagesDirtyRead\": 10,\n        \"searchEngineListTemplate\": \"forSearchEnginesList.htm\",\n        \"searchEngineFileTemplate\": \"forSearchEngines.htm\",\n        \"searchEngineRobotsDb\": \"WEB-INF/robots.db\",\n        \"useDataStore\": true,\n        \"dataStoreClass\": \"org.cofax.SqlDataStore\",\n        \"redirectionClass\": \"org.cofax.SqlRedirection\",\n        \"dataStoreName\": \"cofax\",\n        \"dataStoreDriver\": \"com.microsoft.jdbc.sqlserver.SQLServerDriver\",\n        \"dataStoreUrl\": \"jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon\",\n        \"dataStoreUser\": \"sa\",\n        \"dataStorePassword\": \"dataStoreTestQuery\",\n        \"dataStoreTestQuery\": \"SET NOCOUNT ON;select test='test';\",\n        \"dataStoreLogFile\": \"/usr/local/tomcat/logs/datastore.log\",\n        \"dataStoreInitConns\": 10,\n        \"dataStoreMaxConns\": 100,\n        \"dataStoreConnUsageLimit\": 100,\n        \"dataStoreLogLevel\": \"debug\",\n        \"maxUrlLength\": 500}},\n    {\n      \"servlet-name\": \"cofaxEmail\",\n      \"servlet-class\": \"org.cofax.cds.EmailServlet\",\n      \"init-param\": {\n      \"mailHost\": \"mail1\",\n      \"mailHostOverride\": \"mail2\"}},\n    {\n      \"servlet-name\": \"cofaxAdmin\",\n      \"servlet-class\": \"org.cofax.cds.AdminServlet\"},\n \n    {\n      \"servlet-name\": \"fileServlet\",\n      \"servlet-class\": \"org.cofax.cds.FileServlet\"},\n    {\n      \"servlet-name\": \"cofaxTools\",\n      \"servlet-class\": \"org.cofax.cms.CofaxToolsServlet\",\n      \"init-param\": {\n        \"templatePath\": \"toolstemplates/\",\n        \"log\": 1,\n        \"logLocation\": \"/usr/local/tomcat/logs/CofaxTools.log\",\n        \"logMaxSize\": \"\",\n        \"dataLog\": 1,\n        \"dataLogLocation\": \"/usr/local/tomcat/logs/dataLog.log\",\n        \"dataLogMaxSize\": \"\",\n        \"removePageCache\": \"/content/admin/remove?cache=pages\u0026id=\",\n        \"removeTemplateCache\": \"/content/admin/remove?cache=templates\u0026id=\",\n        \"fileTransferFolder\": \"/usr/local/tomcat/webapps/content/fileTransferFolder\",\n        \"lookInContext\": 1,\n        \"adminGroupID\": 4,\n        \"betaServer\": true}}],\n  \"servlet-mapping\": {\n    \"cofaxCDS\": \"/\",\n    \"cofaxEmail\": \"/cofaxutil/aemail/*\",\n    \"cofaxAdmin\": \"/admin/*\",\n    \"fileServlet\": \"/static/*\",\n    \"cofaxTools\": \"/tools/*\"},\n \n  \"taglib\": {\n    \"taglib-uri\": \"cofax.tld\",\n    \"taglib-location\": \"/WEB-INF/tlds/cofax.tld\"}}}\n```\n\nAnd executing the following:\n\n```php\n    $go = \\DCarbone\\JSONToGO::parse('RootTypeName', $json);\n    \n    file_put_contents(__DIR__.'/example.go', (string)$go);\n```\n\nWill result in (pre `go fmt`):\n\n```go\ntype RootTypeName struct {\n\tWebApp struct {\n\t\t\tServlet []struct {\n\t\t\t\t\tServletName string `json:\"servlet-name\"`\n\t\t\t\t\tServletClass string `json:\"servlet-class\"`\n\t\t\t\t\tInitParam struct {\n\t\t\t\t\t\t\tConfigGlossaryInstallationAt string `json:\"configGlossary:installationAt\"`\n\t\t\t\t\t\t\tConfigGlossaryAdminEmail string `json:\"configGlossary:adminEmail\"`\n\t\t\t\t\t\t\tConfigGlossaryPoweredBy string `json:\"configGlossary:poweredBy\"`\n\t\t\t\t\t\t\tConfigGlossaryPoweredByIcon string `json:\"configGlossary:poweredByIcon\"`\n\t\t\t\t\t\t\tConfigGlossaryStaticPath string `json:\"configGlossary:staticPath\"`\n\t\t\t\t\t\t\tTemplateProcessorClass string `json:\"templateProcessorClass\"`\n\t\t\t\t\t\t\tTemplateLoaderClass string `json:\"templateLoaderClass\"`\n\t\t\t\t\t\t\tTemplatePath string `json:\"templatePath\"`\n\t\t\t\t\t\t\tTemplateOverridePath string `json:\"templateOverridePath\"`\n\t\t\t\t\t\t\tDefaultListTemplate string `json:\"defaultListTemplate\"`\n\t\t\t\t\t\t\tDefaultFileTemplate string `json:\"defaultFileTemplate\"`\n\t\t\t\t\t\t\tUseJSP bool `json:\"useJSP\"`\n\t\t\t\t\t\t\tJspListTemplate string `json:\"jspListTemplate\"`\n\t\t\t\t\t\t\tJspFileTemplate string `json:\"jspFileTemplate\"`\n\t\t\t\t\t\t\tCachePackageTagsTrack int `json:\"cachePackageTagsTrack\"`\n\t\t\t\t\t\t\tCachePackageTagsStore int `json:\"cachePackageTagsStore\"`\n\t\t\t\t\t\t\tCachePackageTagsRefresh int `json:\"cachePackageTagsRefresh\"`\n\t\t\t\t\t\t\tCacheTemplatesTrack int `json:\"cacheTemplatesTrack\"`\n\t\t\t\t\t\t\tCacheTemplatesStore int `json:\"cacheTemplatesStore\"`\n\t\t\t\t\t\t\tCacheTemplatesRefresh int `json:\"cacheTemplatesRefresh\"`\n\t\t\t\t\t\t\tCachePagesTrack int `json:\"cachePagesTrack\"`\n\t\t\t\t\t\t\tCachePagesStore int `json:\"cachePagesStore\"`\n\t\t\t\t\t\t\tCachePagesRefresh int `json:\"cachePagesRefresh\"`\n\t\t\t\t\t\t\tCachePagesDirtyRead int `json:\"cachePagesDirtyRead\"`\n\t\t\t\t\t\t\tSearchEngineListTemplate string `json:\"searchEngineListTemplate\"`\n\t\t\t\t\t\t\tSearchEngineFileTemplate string `json:\"searchEngineFileTemplate\"`\n\t\t\t\t\t\t\tSearchEngineRobotsDb string `json:\"searchEngineRobotsDb\"`\n\t\t\t\t\t\t\tUseDataStore bool `json:\"useDataStore\"`\n\t\t\t\t\t\t\tDataStoreClass string `json:\"dataStoreClass\"`\n\t\t\t\t\t\t\tRedirectionClass string `json:\"redirectionClass\"`\n\t\t\t\t\t\t\tDataStoreName string `json:\"dataStoreName\"`\n\t\t\t\t\t\t\tDataStoreDriver string `json:\"dataStoreDriver\"`\n\t\t\t\t\t\t\tDataStoreURL string `json:\"dataStoreUrl\"`\n\t\t\t\t\t\t\tDataStoreUser string `json:\"dataStoreUser\"`\n\t\t\t\t\t\t\tDataStorePassword string `json:\"dataStorePassword\"`\n\t\t\t\t\t\t\tDataStoreTestQuery string `json:\"dataStoreTestQuery\"`\n\t\t\t\t\t\t\tDataStoreLogFile string `json:\"dataStoreLogFile\"`\n\t\t\t\t\t\t\tDataStoreInitConns int `json:\"dataStoreInitConns\"`\n\t\t\t\t\t\t\tDataStoreMaxConns int `json:\"dataStoreMaxConns\"`\n\t\t\t\t\t\t\tDataStoreConnUsageLimit int `json:\"dataStoreConnUsageLimit\"`\n\t\t\t\t\t\t\tDataStoreLogLevel string `json:\"dataStoreLogLevel\"`\n\t\t\t\t\t\t\tMaxURLLength int `json:\"maxUrlLength\"`\n\t\t\t\t\t\t} `json:\"init-param,omitempty\"`\n\t\t\t\t} `json:\"servlet\"`\n\t\t\tServletMapping struct {\n\t\t\t\t\tCofaxCDS string `json:\"cofaxCDS\"`\n\t\t\t\t\tCofaxEmail string `json:\"cofaxEmail\"`\n\t\t\t\t\tCofaxAdmin string `json:\"cofaxAdmin\"`\n\t\t\t\t\tFileServlet string `json:\"fileServlet\"`\n\t\t\t\t\tCofaxTools string `json:\"cofaxTools\"`\n\t\t\t\t} `json:\"servlet-mapping\"`\n\t\t\tTaglib struct {\n\t\t\t\t\tTaglibURI string `json:\"taglib-uri\"`\n\t\t\t\t\tTaglibLocation string `json:\"taglib-location\"`\n\t\t\t\t} `json:\"taglib\"`\n\t\t} `json:\"web-app\"`\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcarbone%2Fjson-to-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdcarbone%2Fjson-to-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcarbone%2Fjson-to-go/lists"}