定制 dcarbone/json-to-go 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dcarbone/json-to-go

Composer 安装命令:

composer require dcarbone/json-to-go

包简介

PHP Implementation of mholt/json-to-go

README 文档

README

PHP Implementation of mholt/json-to-go

Composer

{
    "require": {
        "dcarbone/json-to-go": "@stable"
    }
}

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:

$jsonToGO = \DCarbone\JSONToGO::parse('RootTypeName', $myjson);

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 becomes Eight_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 the json:,omitempty markup at the end of struct properties
  • forceIntToFloat - Will convert all int types to float.
  • useSimpleInt - Will, if using ints, use the simple int type rather than attempting to determine int32 vs int64
  • forceScalarToPointer - Will turn all simple types (string, int, float, bool) into pointers
  • emptyStructToInterface - Will convert an object without properties into an interface{}
  • breakOutInlineStructs - Will create bespoke type definitions for nested objects
  • sanitizeInput - 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 struct
  • callbacks - Instance of Callbacks or array of ['callback' => callable] where callback == name of parameter on Callbacks and callable == 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 MapType and 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):

{"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://LOCALHOST: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"}}}

And executing the following:

    $go = \DCarbone\JSONToGO::parse('RootTypeName', $json);
    
    file_put_contents(__DIR__.'/example.go', (string)$go);

Will result in (pre 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"`
}

dcarbone/json-to-go 适用场景与选型建议

dcarbone/json-to-go 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 161 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 10 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「go」 「golang」 「json to go」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dcarbone/json-to-go 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dcarbone/json-to-go 我们能提供哪些服务?
定制开发 / 二次开发

基于 dcarbone/json-to-go 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 161
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-10-27