{"id":473652,"date":"2025-09-02T17:12:31","date_gmt":"2025-09-02T17:12:31","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=473652"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=473652","title":{"rendered":"<span>\u041f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u0438\u0437\u043c \u0432 JSON \u0432 Go<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/8da\/ec8\/bff\/8daec8bff6ffb0b08b851164716d84cc.png\" alt=\"PolyJSON\" title=\"PolyJSON\" width=\"1488\" height=\"882\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/8da\/ec8\/bff\/8daec8bff6ffb0b08b851164716d84cc.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/8da\/ec8\/bff\/8daec8bff6ffb0b08b851164716d84cc.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>PolyJSON<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u043d\u0430\u044f \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f JSON \u2014 \u0447\u0430\u0441\u0442\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043f\u0440\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 API, UI-\u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0438\u043b\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440. \u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b:<\/p>\n<pre><code class=\"json\">[   {\"type\": \"text\", \"content\": \"hello\"},   {\"type\": \"image\", \"url\": \"pic.jpg\"} ]<\/code><\/pre>\n<p>\u0412 Go \u0442\u0430\u043a\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432. \u041e\u0434\u043d\u0430\u043a\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043f\u0430\u043a\u0435\u0442 <code>encoding\/json<\/code> \u043d\u0435 \u0443\u043c\u0435\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0441 \u043f\u043e\u043b\u0435\u043c-\u0434\u0438\u0441\u043a\u0440\u0438\u043c\u0438\u043d\u0430\u0442\u043e\u0440\u043e\u043c (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>\"type\"<\/code>), \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043f\u043e\u0434\u0442\u0438\u043f. \u041f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u0432\u0440\u043e\u0434\u0435 <code>map[string]any<\/code> \u0438\u043b\u0438 <code>json.RawMessage<\/code> , \u043b\u0438\u0431\u043e \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b <code>json.Marshaler<\/code> \u0438 <code>json.Unmarshaler<\/code> \u0441 \u0440\u0430\u0437\u0431\u043e\u0440\u043e\u043c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u2014 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0431\u044b\u0441\u0442\u0440\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0438 \u0441\u043b\u0430\u0431\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u044b\u043b\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b \u0434\u0432\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/github.com\/ykalchevskiy\/poly\" rel=\"noopener noreferrer nofollow\"><code>poly<\/code><\/a> \u2014 \u043e\u0431\u0451\u0440\u0442\u043a\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432;<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/ykalchevskiy\/polygen\" rel=\"noopener noreferrer nofollow\"><code>polygen<\/code><\/a> \u2014 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u043a\u043e\u0434\u0430, \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u044e\u0449\u0438\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 <code>poly<\/code>.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 poly<\/h3>\n<p><code>poly<\/code> \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0438 \u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e JSON \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 \u0438 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432. \u041f\u043e\u0434\u0442\u0438\u043f\u044b \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 <code>poly.TypesN[...]<\/code> \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 <code>poly.TypeName<\/code> \u0441 \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>TypeName() string<\/code>, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f <code>\"type\"<\/code>.<\/p>\n<h4>\u041e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u043e\u0432<\/h4>\n<pre><code class=\"go\">type Item = poly.Poly[IsItem, poly.Types2[TextItem, ImageItem]]  type IsItem interface { poly.TypeName  \/\/ \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u044f\u0432\u043d\u043e, \u043d\u043e \u0443\u0434\u043e\u0431\u043d\u043e isItem() }  type TextItem struct { Content string `json:\"content\"` }  func (TextItem) isItem() {} func (TextItem) TypeName() string { return \"text\" }  type ImageItem struct { URL string `json:\"url\"` }  func (ImageItem) isItem() {} func (ImageItem) TypeName() string { return \"image\" }<\/code><\/pre>\n<h4>\u0414\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h4>\n<pre><code class=\"go\">var item Item  _ = json.Unmarshal([]byte(`{\"type\":\"text\",\"content\":\"hello\"}`), &amp;item) \/\/ item.Value =&gt; TextItem{Content: \"hello\"}  _ = json.Unmarshal([]byte(`{\"type\":\"image\",\"url\":\"pic.jpg\"}`), &amp;item) \/\/ item.Value =&gt; ImageItem{URL: \"pic.jpg\"}  _ = json.Unmarshal([]byte(`{\"url\":\"new.jpg\"}`), &amp;item) \/\/ item.Value =&gt; ImageItem{URL: \"new.jpg\"}<\/code><\/pre>\n<h4>\u0421\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h4>\n<pre><code class=\"go\">item = Item{Value: TextItem{Content: \"Hi\"}} data, _ := json.Marshal(item) \/\/ {\"type\":\"text\",\"content\":\"Hi\"}  item = Item{Value: ImageItem{URL: \"pic.jpg\"}} data, _ = json.Marshal(item) \/\/ {\"type\":\"image\",\"url\":\"pic.jpg\"} <\/code><\/pre>\n<h3>\u0417\u0430\u0447\u0435\u043c \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f polygen<\/h3>\n<p><code>poly<\/code> \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u043b\u0441\u044f \u043a\u0430\u043a \u043b\u0451\u0433\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u0435\u0437 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u043e \u043c\u0435\u0440\u0435 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u044f \u0441\u0442\u0430\u043b\u043e \u044f\u0441\u043d\u043e, \u0447\u0442\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043d\u0435 \u0443\u0434\u0430\u0451\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0435\u0437 \u0443\u0441\u043b\u043e\u0436\u043d\u0435\u043d\u0438\u044f API:<\/p>\n<ul>\n<li>\n<p>\u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0438\u043c\u0435\u043d\u0438 \u043f\u043e\u043b\u044f-\u0434\u0438\u0441\u043a\u0440\u0438\u043c\u0438\u043d\u0430\u0442\u043e\u0440\u0430;<\/p>\n<\/li>\n<li>\n<p>\u0441\u0442\u0440\u043e\u0433\u0438\u0439 \u0440\u0435\u0436\u0438\u043c (\u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u043c \u043f\u043e\u043b\u0435 (DisallowUnknownFields));<\/p>\n<\/li>\n<li>\n<p>\u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043f\u0440\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0438 <code>\"type\"<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432.<\/p>\n<\/li>\n<\/ul>\n<p>\u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u0442\u044c <code>poly<\/code>, \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u2014 <a href=\"https:\/\/github.com\/ykalchevskiy\/polygen\" rel=\"noopener noreferrer nofollow\"><code>polygen<\/code><\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0448\u0430\u0435\u0442 \u044d\u0442\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u0447\u0435\u0440\u0435\u0437 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u043a\u043e\u0434\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0444\u0430\u0439\u043b\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438.<\/p>\n<h4>\u041e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u043e\u0432<\/h4>\n<pre><code class=\"go\">type IsItem interface { isItem() }  type TextItem struct { Content string `json:\"content\"` }  func (TextItem) isItem() {}  type ImageItem struct { URL string `json:\"url\"` }  func (ImageItem) isItem() {}<\/code><\/pre>\n<h4>\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f .polygen.json<\/h4>\n<pre><code class=\"json\">{   \"$schema\": \"https:\/\/raw.githubusercontent.com\/ykalchevskiy\/polygen\/refs\/heads\/main\/schema.json\",   \"types\": [     {       \"type\": \"Item\",       \"interface\": \"IsItem\",       \"package\": \"main\",       \"subtypes\": {         \"TextItem\": {           \"name\": \"text\"         },         \"ImageItem\": {           \"name\": \"image\"         }       }     }   ] }<\/code><\/pre>\n<p>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u0438 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 README \u0438 \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.<\/p>\n<h4>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f<\/h4>\n<pre><code class=\"bash\">$ go install github.com\/ykalchevskiy\/polygen@latest $ polygen<\/code><\/pre>\n<p>\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0444\u0430\u0439\u043b <code>item_polygen.go<\/code> \u0441 \u0442\u0438\u043f\u043e\u043c <code>Item<\/code>, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u043c \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e\/\u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043f\u043e \u043f\u043e\u043b\u044e <code>\"type\"<\/code>.<\/p>\n<h4>\u0414\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h4>\n<pre><code class=\"go\">var item Item  _ = json.Unmarshal([]byte(`{\"type\": \"text\", \"content\": \"hello\"}`), &amp;item) \/\/ item.IsItem =&gt; TextItem{Content: \"hello\"}  _ = json.Unmarshal([]byte(`{\"type\": \"image\", \"url\": \"pic.jpg\"}`), &amp;item) \/\/ item.IsItem =&gt; ImageItem{URL: \"pic.jpg\"}  _ = json.Unmarshal([]byte(`{\"url\": \"new.jpg\"}`), &amp;item) \/\/ item.IsItem =&gt; ImageItem{URL: \"new.jpg\"}<\/code><\/pre>\n<h4>\u0421\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h4>\n<pre><code class=\"go\">item = Item{IsItem: TextItem{Content: \"Hi\"}} data, _ := json.Marshal(item) \/\/ {\"type\":\"text\",\"content\":\"Hi\"}  item = Item{IsItem: ImageItem{URL: \"pic.jpg\"}} data, _ = json.Marshal(item) \/\/ {\"type\":\"image\",\"url\":\"pic.jpg\"}<\/code><\/pre>\n<h3>\u0421\u0441\u044b\u043b\u043a\u0438<\/h3>\n<ul>\n<li>\n<p><code>poly<\/code>: <a href=\"https:\/\/github.com\/ykalchevskiy\/poly\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/ykalchevskiy\/poly<\/a><\/p>\n<\/li>\n<li>\n<p><code>polygen<\/code>: <a href=\"https:\/\/github.com\/ykalchevskiy\/polygen\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/ykalchevskiy\/polygen<\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/927732\/\"> https:\/\/habr.com\/ru\/articles\/927732\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\">\n<div><figcaption>PolyJSON<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u043d\u0430\u044f \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f JSON \u2014 \u0447\u0430\u0441\u0442\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043f\u0440\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 API, UI-\u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0438\u043b\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440. \u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b:<\/p>\n<pre><code class=\"json\">[   {\"type\": \"text\", \"content\": \"hello\"},   {\"type\": \"image\", \"url\": \"pic.jpg\"} ]<\/code><\/pre>\n<p>\u0412 Go \u0442\u0430\u043a\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432. \u041e\u0434\u043d\u0430\u043a\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043f\u0430\u043a\u0435\u0442 <code>encoding\/json<\/code> \u043d\u0435 \u0443\u043c\u0435\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0441 \u043f\u043e\u043b\u0435\u043c-\u0434\u0438\u0441\u043a\u0440\u0438\u043c\u0438\u043d\u0430\u0442\u043e\u0440\u043e\u043c (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>\"type\"<\/code>), \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043f\u043e\u0434\u0442\u0438\u043f. \u041f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u0432\u0440\u043e\u0434\u0435 <code>map[string]any<\/code> \u0438\u043b\u0438 <code>json.RawMessage<\/code> , \u043b\u0438\u0431\u043e \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b <code>json.Marshaler<\/code> \u0438 <code>json.Unmarshaler<\/code> \u0441 \u0440\u0430\u0437\u0431\u043e\u0440\u043e\u043c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u2014 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0431\u044b\u0441\u0442\u0440\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0438 \u0441\u043b\u0430\u0431\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u044b\u043b\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b \u0434\u0432\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/github.com\/ykalchevskiy\/poly\" rel=\"noopener noreferrer nofollow\"><code>poly<\/code><\/a> \u2014 \u043e\u0431\u0451\u0440\u0442\u043a\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432;<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/ykalchevskiy\/polygen\" rel=\"noopener noreferrer nofollow\"><code>polygen<\/code><\/a> \u2014 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u043a\u043e\u0434\u0430, \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u044e\u0449\u0438\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 <code>poly<\/code>.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 poly<\/h3>\n<p><code>poly<\/code> \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0438 \u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e JSON \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 \u0438 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432. \u041f\u043e\u0434\u0442\u0438\u043f\u044b \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 <code>poly.TypesN[...]<\/code> \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 <code>poly.TypeName<\/code> \u0441 \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>TypeName() string<\/code>, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f <code>\"type\"<\/code>.<\/p>\n<h4>\u041e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u043e\u0432<\/h4>\n<pre><code class=\"go\">type Item = poly.Poly[IsItem, poly.Types2[TextItem, ImageItem]]  type IsItem interface { poly.TypeName  \/\/ \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u044f\u0432\u043d\u043e, \u043d\u043e \u0443\u0434\u043e\u0431\u043d\u043e isItem() }  type TextItem struct { Content string `json:\"content\"` }  func (TextItem) isItem() {} func (TextItem) TypeName() string { return \"text\" }  type ImageItem struct { URL string `json:\"url\"` }  func (ImageItem) isItem() {} func (ImageItem) TypeName() string { return \"image\" }<\/code><\/pre>\n<h4>\u0414\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h4>\n<pre><code class=\"go\">var item Item  _ = json.Unmarshal([]byte(`{\"type\":\"text\",\"content\":\"hello\"}`), &amp;item) \/\/ item.Value =&gt; TextItem{Content: \"hello\"}  _ = json.Unmarshal([]byte(`{\"type\":\"image\",\"url\":\"pic.jpg\"}`), &amp;item) \/\/ item.Value =&gt; ImageItem{URL: \"pic.jpg\"}  _ = json.Unmarshal([]byte(`{\"url\":\"new.jpg\"}`), &amp;item) \/\/ item.Value =&gt; ImageItem{URL: \"new.jpg\"}<\/code><\/pre>\n<h4>\u0421\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h4>\n<pre><code class=\"go\">item = Item{Value: TextItem{Content: \"Hi\"}} data, _ := json.Marshal(item) \/\/ {\"type\":\"text\",\"content\":\"Hi\"}  item = Item{Value: ImageItem{URL: \"pic.jpg\"}} data, _ = json.Marshal(item) \/\/ {\"type\":\"image\",\"url\":\"pic.jpg\"} <\/code><\/pre>\n<h3>\u0417\u0430\u0447\u0435\u043c \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f polygen<\/h3>\n<p><code>poly<\/code> \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u043b\u0441\u044f \u043a\u0430\u043a \u043b\u0451\u0433\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u0435\u0437 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u043e \u043c\u0435\u0440\u0435 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u044f \u0441\u0442\u0430\u043b\u043e \u044f\u0441\u043d\u043e, \u0447\u0442\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043d\u0435 \u0443\u0434\u0430\u0451\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0435\u0437 \u0443\u0441\u043b\u043e\u0436\u043d\u0435\u043d\u0438\u044f API:<\/p>\n<ul>\n<li>\n<p>\u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0438\u043c\u0435\u043d\u0438 \u043f\u043e\u043b\u044f-\u0434\u0438\u0441\u043a\u0440\u0438\u043c\u0438\u043d\u0430\u0442\u043e\u0440\u0430;<\/p>\n<\/li>\n<li>\n<p>\u0441\u0442\u0440\u043e\u0433\u0438\u0439 \u0440\u0435\u0436\u0438\u043c (\u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u043c \u043f\u043e\u043b\u0435 (DisallowUnknownFields));<\/p>\n<\/li>\n<li>\n<p>\u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043f\u0440\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0438 <code>\"type\"<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432.<\/p>\n<\/li>\n<\/ul>\n<p>\u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u0442\u044c <code>poly<\/code>, \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u2014 <a href=\"https:\/\/github.com\/ykalchevskiy\/polygen\" rel=\"noopener noreferrer nofollow\"><code>polygen<\/code><\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0448\u0430\u0435\u0442 \u044d\u0442\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u0447\u0435\u0440\u0435\u0437 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u043a\u043e\u0434\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0444\u0430\u0439\u043b\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438.<\/p>\n<h4>\u041e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u043e\u0432<\/h4>\n<pre><code class=\"go\">type IsItem interface { isItem() }  type TextItem struct { Content string `json:\"content\"` }  func (TextItem) isItem() {}  type ImageItem struct { URL string `json:\"url\"` }  func (ImageItem) isItem() {}<\/code><\/pre>\n<h4>\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f .polygen.json<\/h4>\n<pre><code class=\"json\">{   \"$schema\": \"https:\/\/raw.githubusercontent.com\/ykalchevskiy\/polygen\/refs\/heads\/main\/schema.json\",   \"types\": [     {       \"type\": \"Item\",       \"interface\": \"IsItem\",       \"package\": \"main\",       \"subtypes\": {         \"TextItem\": {           \"name\": \"text\"         },         \"ImageItem\": {           \"name\": \"image\"         }       }     }   ] }<\/code><\/pre>\n<p>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u0438 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 README \u0438 \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.<\/p>\n<h4>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f<\/h4>\n<pre><code class=\"bash\">$ go install github.com\/ykalchevskiy\/polygen@latest $ polygen<\/code><\/pre>\n<p>\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0444\u0430\u0439\u043b <code>item_polygen.go<\/code> \u0441 \u0442\u0438\u043f\u043e\u043c <code>Item<\/code>, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u043c \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e\/\u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043f\u043e \u043f\u043e\u043b\u044e <code>\"type\"<\/code>.<\/p>\n<h4>\u0414\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h4>\n<pre><code class=\"go\">var item Item  _ = json.Unmarshal([]byte(`{\"type\": \"text\", \"content\": \"hello\"}`), &amp;item) \/\/ item.IsItem =&gt; TextItem{Content: \"hello\"}  _ = json.Unmarshal([]byte(`{\"type\": \"image\", \"url\": \"pic.jpg\"}`), &amp;item) \/\/ item.IsItem =&gt; ImageItem{URL: \"pic.jpg\"}  _ = json.Unmarshal([]byte(`{\"url\": \"new.jpg\"}`), &amp;item) \/\/ item.IsItem =&gt; ImageItem{URL: \"new.jpg\"}<\/code><\/pre>\n<h4>\u0421\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h4>\n<pre><code class=\"go\">item = Item{IsItem: TextItem{Content: \"Hi\"}} data, _ := json.Marshal(item) \/\/ {\"type\":\"text\",\"content\":\"Hi\"}  item = Item{IsItem: ImageItem{URL: \"pic.jpg\"}} data, _ = json.Marshal(item) \/\/ {\"type\":\"image\",\"url\":\"pic.jpg\"}<\/code><\/pre>\n<h3>\u0421\u0441\u044b\u043b\u043a\u0438<\/h3>\n<ul>\n<li>\n<p><code>poly<\/code>: <a href=\"https:\/\/github.com\/ykalchevskiy\/poly\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/ykalchevskiy\/poly<\/a><\/p>\n<\/li>\n<li>\n<p><code>polygen<\/code>: <a href=\"https:\/\/github.com\/ykalchevskiy\/polygen\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/ykalchevskiy\/polygen<\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/927732\/\"> https:\/\/habr.com\/ru\/articles\/927732\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-473652","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/473652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=473652"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/473652\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=473652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=473652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=473652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}