dcarbone / json-to-go
mholt/json-to-go的PHP实现
0.8.0
2017-08-03 19:10 UTC
Requires
- php: ^7.1
- psr/log: @stable
This package is auto-updated.
Last update: 2024-09-17 09:30:35 UTC
README
mholt/json-to-go的PHP实现
Composer
{
"require": {
"dcarbone/json-to-go": "@stable"
}
}
为什么用PHP实现?
因为这样更适合我的个人工作流程。
也因为为什么不呢。
基本用法
一旦将其包含到项目中,最简单的方法可能是使用静态初始化器
$jsonToGO = \DCarbone\JSONToGO::parse('RootTypeName', $myjson);
这将返回一个解析后的输入的AbstractType实例。如果在解析过程中出现错误,将抛出异常。
有关可能的类型完整列表,请参阅此处
此类实现了__toString(),返回值是解析后的GO对象。
通用规则
- 如果一个对象的属性名完全由数字组成,它将被添加前缀
Num - 如果一个对象的属性名以非字母数字字符开头,它将被添加前缀
X - 如果一个对象的属性名以数字开头,该字符将被转换为字符串,具体转换规则请参考此处。例如:`80211X`变为`Eight_0211X`。您可以在初始化配置类时定义自己的映射。
- 如果一个类型不可用(例如json中的NULL),或者如果映射中的键之间存在值类型冲突,则该类型将被定义为`interface{}`
- 事物的名称始终是导出的
- 始终运行结果通过
go fmt是一个好主意
自定义配置
在解析JSON时,有一些可能的选项可以在配置类中定义
forceOmitEmpty- 总是在结构属性末尾放置json:,omitempty标记forceIntToFloat- 将所有int类型转换为float。useSimpleInt- 如果使用int,则使用简单的int类型而不是尝试确定int32与int64forceScalarToPointer- 将所有简单类型(字符串、int、float、bool)转换为指针emptyStructToInterface- 将没有属性的对象转换为interface{}breakOutInlineStructs- 为嵌套对象创建专有的类型定义sanitizeInput- 覆盖示例json中的值(请参阅此处)initialNumberMap- 用于在结构开始时将数字字符转换为字母字符的数组callbacks- Callbacks的实例或包含['callback' => callable]的数组,其中callback是Callbacks的参数名称,而callable是接受与函数名称相同的属性参数的任何有效PHP可调用函数。
映射和原始消息类型
默认情况下,不会使用MapType和RawMessageType。要利用这些类型,必须实现自己的goType,该函数分别返回map或raw。
示例
以这个JSON负载为例(来源)
{"web-app": {
"servlet": [
{
"servlet-name": "cofaxCDS",
"servlet-class": "org.cofax.cds.CDSServlet",
"init-param": {
"configGlossary:installationAt": "Philadelphia, PA",
"configGlossary:adminEmail": "ksm@pobox.com",
"configGlossary:poweredBy": "Cofax",
"configGlossary:poweredByIcon": "/images/cofax.gif",
"configGlossary:staticPath": "/content/static",
"templateProcessorClass": "org.cofax.WysiwygTemplate",
"templateLoaderClass": "org.cofax.FilesTemplateLoader",
"templatePath": "templates",
"templateOverridePath": "",
"defaultListTemplate": "listTemplate.htm",
"defaultFileTemplate": "articleTemplate.htm",
"useJSP": false,
"jspListTemplate": "listTemplate.jsp",
"jspFileTemplate": "articleTemplate.jsp",
"cachePackageTagsTrack": 200,
"cachePackageTagsStore": 200,
"cachePackageTagsRefresh": 60,
"cacheTemplatesTrack": 100,
"cacheTemplatesStore": 50,
"cacheTemplatesRefresh": 15,
"cachePagesTrack": 200,
"cachePagesStore": 100,
"cachePagesRefresh": 10,
"cachePagesDirtyRead": 10,
"searchEngineListTemplate": "forSearchEnginesList.htm",
"searchEngineFileTemplate": "forSearchEngines.htm",
"searchEngineRobotsDb": "WEB-INF/robots.db",
"useDataStore": true,
"dataStoreClass": "org.cofax.SqlDataStore",
"redirectionClass": "org.cofax.SqlRedirection",
"dataStoreName": "cofax",
"dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
"dataStoreUrl": "jdbc:microsoft:sqlserver://:1433;DatabaseName=goon",
"dataStoreUser": "sa",
"dataStorePassword": "dataStoreTestQuery",
"dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
"dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
"dataStoreInitConns": 10,
"dataStoreMaxConns": 100,
"dataStoreConnUsageLimit": 100,
"dataStoreLogLevel": "debug",
"maxUrlLength": 500}},
{
"servlet-name": "cofaxEmail",
"servlet-class": "org.cofax.cds.EmailServlet",
"init-param": {
"mailHost": "mail1",
"mailHostOverride": "mail2"}},
{
"servlet-name": "cofaxAdmin",
"servlet-class": "org.cofax.cds.AdminServlet"},
{
"servlet-name": "fileServlet",
"servlet-class": "org.cofax.cds.FileServlet"},
{
"servlet-name": "cofaxTools",
"servlet-class": "org.cofax.cms.CofaxToolsServlet",
"init-param": {
"templatePath": "toolstemplates/",
"log": 1,
"logLocation": "/usr/local/tomcat/logs/CofaxTools.log",
"logMaxSize": "",
"dataLog": 1,
"dataLogLocation": "/usr/local/tomcat/logs/dataLog.log",
"dataLogMaxSize": "",
"removePageCache": "/content/admin/remove?cache=pages&id=",
"removeTemplateCache": "/content/admin/remove?cache=templates&id=",
"fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder",
"lookInContext": 1,
"adminGroupID": 4,
"betaServer": true}}],
"servlet-mapping": {
"cofaxCDS": "/",
"cofaxEmail": "/cofaxutil/aemail/*",
"cofaxAdmin": "/admin/*",
"fileServlet": "/static/*",
"cofaxTools": "/tools/*"},
"taglib": {
"taglib-uri": "cofax.tld",
"taglib-location": "/WEB-INF/tlds/cofax.tld"}}}
执行以下操作
$go = \DCarbone\JSONToGO::parse('RootTypeName', $json); file_put_contents(__DIR__.'/example.go', (string)$go);
将得到结果(在go fmt之前)
type RootTypeName struct { WebApp struct { Servlet []struct { ServletName string `json:"servlet-name"` ServletClass string `json:"servlet-class"` InitParam struct { ConfigGlossaryInstallationAt string `json:"configGlossary:installationAt"` ConfigGlossaryAdminEmail string `json:"configGlossary:adminEmail"` ConfigGlossaryPoweredBy string `json:"configGlossary:poweredBy"` ConfigGlossaryPoweredByIcon string `json:"configGlossary:poweredByIcon"` ConfigGlossaryStaticPath string `json:"configGlossary:staticPath"` TemplateProcessorClass string `json:"templateProcessorClass"` TemplateLoaderClass string `json:"templateLoaderClass"` TemplatePath string `json:"templatePath"` TemplateOverridePath string `json:"templateOverridePath"` DefaultListTemplate string `json:"defaultListTemplate"` DefaultFileTemplate string `json:"defaultFileTemplate"` UseJSP bool `json:"useJSP"` JspListTemplate string `json:"jspListTemplate"` JspFileTemplate string `json:"jspFileTemplate"` CachePackageTagsTrack int `json:"cachePackageTagsTrack"` CachePackageTagsStore int `json:"cachePackageTagsStore"` CachePackageTagsRefresh int `json:"cachePackageTagsRefresh"` CacheTemplatesTrack int `json:"cacheTemplatesTrack"` CacheTemplatesStore int `json:"cacheTemplatesStore"` CacheTemplatesRefresh int `json:"cacheTemplatesRefresh"` CachePagesTrack int `json:"cachePagesTrack"` CachePagesStore int `json:"cachePagesStore"` CachePagesRefresh int `json:"cachePagesRefresh"` CachePagesDirtyRead int `json:"cachePagesDirtyRead"` SearchEngineListTemplate string `json:"searchEngineListTemplate"` SearchEngineFileTemplate string `json:"searchEngineFileTemplate"` SearchEngineRobotsDb string `json:"searchEngineRobotsDb"` UseDataStore bool `json:"useDataStore"` DataStoreClass string `json:"dataStoreClass"` RedirectionClass string `json:"redirectionClass"` DataStoreName string `json:"dataStoreName"` DataStoreDriver string `json:"dataStoreDriver"` DataStoreURL string `json:"dataStoreUrl"` DataStoreUser string `json:"dataStoreUser"` DataStorePassword string `json:"dataStorePassword"` DataStoreTestQuery string `json:"dataStoreTestQuery"` DataStoreLogFile string `json:"dataStoreLogFile"` DataStoreInitConns int `json:"dataStoreInitConns"` DataStoreMaxConns int `json:"dataStoreMaxConns"` DataStoreConnUsageLimit int `json:"dataStoreConnUsageLimit"` DataStoreLogLevel string `json:"dataStoreLogLevel"` MaxURLLength int `json:"maxUrlLength"` } `json:"init-param,omitempty"` } `json:"servlet"` ServletMapping struct { CofaxCDS string `json:"cofaxCDS"` CofaxEmail string `json:"cofaxEmail"` CofaxAdmin string `json:"cofaxAdmin"` FileServlet string `json:"fileServlet"` CofaxTools string `json:"cofaxTools"` } `json:"servlet-mapping"` Taglib struct { TaglibURI string `json:"taglib-uri"` TaglibLocation string `json:"taglib-location"` } `json:"taglib"` } `json:"web-app"` }