From fd8bfbb100a0523846df3fb83875fd2e5f3cd8c2 Mon Sep 17 00:00:00 2001 From: Max Cherry Date: Sat, 3 Mar 2018 09:49:12 +0200 Subject: [PATCH] Switch from custom RawJSON to json.RawMessage https://github.com/mxmCherry/openrtb/issues/23 --- app.go | 4 ++- audio.go | 4 ++- banner.go | 4 ++- bid.go | 4 ++- bid_request.go | 4 ++- bid_response.go | 4 ++- content.go | 4 ++- data.go | 4 ++- deal.go | 4 ++- device.go | 4 ++- format.go | 4 ++- geo.go | 4 ++- imp.go | 4 ++- metric.go | 4 ++- native.go | 4 ++- native/request/asset.go | 4 ++- native/request/data.go | 8 +++-- native/request/event_tracker.go | 8 +++-- native/request/image.go | 8 +++-- native/request/raw_json.go | 27 --------------- native/request/raw_json_test.go | 57 -------------------------------- native/request/request.go | 8 +++-- native/request/title.go | 4 ++- native/request/video.go | 4 ++- native/response/asset.go | 4 ++- native/response/data.go | 8 +++-- native/response/event_tracker.go | 10 ++++-- native/response/image.go | 8 +++-- native/response/link.go | 4 ++- native/response/raw_json.go | 27 --------------- native/response/raw_json_test.go | 57 -------------------------------- native/response/response.go | 4 ++- native/response/title.go | 4 ++- pmp.go | 4 ++- producer.go | 4 ++- publisher.go | 4 ++- raw_json.go | 27 --------------- raw_json_test.go | 57 -------------------------------- regs.go | 4 ++- seat_bid.go | 4 ++- segment.go | 4 ++- site.go | 4 ++- source.go | 4 ++- user.go | 4 ++- video.go | 4 ++- 45 files changed, 139 insertions(+), 299 deletions(-) delete mode 100644 native/request/raw_json.go delete mode 100644 native/request/raw_json_test.go delete mode 100644 native/response/raw_json.go delete mode 100644 native/response/raw_json_test.go delete mode 100644 raw_json.go delete mode 100644 raw_json_test.go diff --git a/app.go b/app.go index c9e266d..589dace 100644 --- a/app.go +++ b/app.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.14 Object: App // // This object should be included if the ad supported content is a non-browser application (typically in mobile) as opposed to a website. @@ -130,5 +132,5 @@ type App struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/audio.go b/audio.go index 2517011..ddca515 100644 --- a/audio.go +++ b/audio.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.8 Object: Audio // // This object represents an audio type impression. @@ -178,5 +180,5 @@ type Audio struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/banner.go b/banner.go index f776924..70968fc 100644 --- a/banner.go +++ b/banner.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.6 Object: Banner // // This object represents the most general type of impression. @@ -165,5 +167,5 @@ type Banner struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/bid.go b/bid.go index 50f3426..956d7bb 100644 --- a/bid.go +++ b/bid.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 4.2.3 Object: Bid // // A SeatBid object contains one or more Bid objects, each of which relates to a specific impression in the bid request via the impid attribute and constitutes an offer to buy that impression for a given price. @@ -273,5 +275,5 @@ type Bid struct { // object // Description: // Placeholder for bidder-specific extensions to OpenRTB - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/bid_request.go b/bid_request.go index d4a8988..6ac82dc 100644 --- a/bid_request.go +++ b/bid_request.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.1 Object: BidRequest // // The top-level bid request object contains a globally unique bid request or auction ID. @@ -205,5 +207,5 @@ type BidRequest struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/bid_response.go b/bid_response.go index e0aab44..a9572cf 100644 --- a/bid_response.go +++ b/bid_response.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 4.2.1 Object: BidResponse // // This object is the top-level bid response object (i.e., the unnamed outer JSON object). @@ -70,5 +72,5 @@ type BidResponse struct { // object // Description: // Placeholder for bidder-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/content.go b/content.go index d0998b5..395abed 100644 --- a/content.go +++ b/content.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.16 Object: Content // // This object describes the content in which the impression will appear, which may be syndicated or nonsyndicated content. @@ -226,5 +228,5 @@ type Content struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/data.go b/data.go index a0b4fb7..77387a7 100644 --- a/data.go +++ b/data.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.21 Object: Data // // The data and segment objects together allow additional data about the related object (e.g., user, content) to be specified. @@ -39,5 +41,5 @@ type Data struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/deal.go b/deal.go index 7db536a..98bf652 100644 --- a/deal.go +++ b/deal.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.12 Object: Deal // // This object constitutes a specific deal that was struck a priori between a buyer and a seller. @@ -70,5 +72,5 @@ type Deal struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/device.go b/device.go index db9eda8..fc3b527 100644 --- a/device.go +++ b/device.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.18 Object: Device // // This object provides information pertaining to the device through which the user is interacting. @@ -271,5 +273,5 @@ type Device struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/format.go b/format.go index cafd08e..f15835c 100644 --- a/format.go +++ b/format.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.10 Object: Format // // This object represents an allowed size (i.e., height and width combination) or Flex Ad parameters for a banner impression. @@ -54,5 +56,5 @@ type Format struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/geo.go b/geo.go index 63afe1b..a709449 100644 --- a/geo.go +++ b/geo.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.19 Object: Geo // // This object encapsulates various methods for specifying a geographic location. @@ -133,5 +135,5 @@ type Geo struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/imp.go b/imp.go index 327b0aa..b636845 100644 --- a/imp.go +++ b/imp.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.4 Object: Imp // // This object describes an ad placement or impression being auctioned. @@ -176,5 +178,5 @@ type Imp struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/metric.go b/metric.go index 000b016..e160860 100644 --- a/metric.go +++ b/metric.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.5 Object: Metric // // This object is associated with an impression as an array of metrics. @@ -42,5 +44,5 @@ type Metric struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native.go b/native.go index 15920d1..377d865 100644 --- a/native.go +++ b/native.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.9 Object: Native // // This object represents a native type impression. @@ -57,5 +59,5 @@ type Native struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/request/asset.go b/native/request/asset.go index 0585930..6cd78db 100644 --- a/native/request/asset.go +++ b/native/request/asset.go @@ -1,5 +1,7 @@ package request +import "encoding/json" + // 4.2 Asset Object // // The main container object for each asset requested or supported by Exchange on behalf of the rendering client. @@ -99,5 +101,5 @@ type Asset struct { // object // Description: // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/request/data.go b/native/request/data.go index e89c115..1f9d31e 100644 --- a/native/request/data.go +++ b/native/request/data.go @@ -1,6 +1,10 @@ package request -import "github.com/mxmCherry/openrtb/native" +import ( + "encoding/json" + + "github.com/mxmCherry/openrtb/native" +) // 4.6 Data Object // @@ -39,5 +43,5 @@ type Data struct { // object // Description: // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/request/event_tracker.go b/native/request/event_tracker.go index f73c829..1e59c1e 100644 --- a/native/request/event_tracker.go +++ b/native/request/event_tracker.go @@ -1,6 +1,10 @@ package request -import "github.com/mxmCherry/openrtb/native" +import ( + "encoding/json" + + "github.com/mxmCherry/openrtb/native" +) // 4.7 Event Trackers Request Object // @@ -37,5 +41,5 @@ type EventTracker struct { // object // Description: // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/request/image.go b/native/request/image.go index c28864d..17d6c13 100644 --- a/native/request/image.go +++ b/native/request/image.go @@ -1,6 +1,10 @@ package request -import "github.com/mxmCherry/openrtb/native" +import ( + "encoding/json" + + "github.com/mxmCherry/openrtb/native" +) // 4.4 Image Object // @@ -90,5 +94,5 @@ type Image struct { // object // Description: // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/request/raw_json.go b/native/request/raw_json.go deleted file mode 100644 index d2d785a..0000000 --- a/native/request/raw_json.go +++ /dev/null @@ -1,27 +0,0 @@ -package request - -import "errors" - -// RawJSON is a raw encoded JSON value. -// It implements encoding/json.Marshaler and encoding/json.Unmarshaler and can -// be used to delay JSON decoding or precompute a JSON encoding. -// -// Basically, it's just a copy of encoding/json.RawMessage type, -// but with more convenient non-pointer encoding. -// -// HEADS UP: this will be replaced with json.RawMessage when Go 1.10 is out. -type RawJSON []byte - -// MarshalJSON returns m as the JSON encoding of m. -func (m RawJSON) MarshalJSON() ([]byte, error) { - return m, nil -} - -// UnmarshalJSON sets *m to a copy of data. -func (m *RawJSON) UnmarshalJSON(data []byte) error { - if m == nil { - return errors.New("openrtb/native/request.RawJSON: UnmarshalJSON on nil pointer") - } - *m = append((*m)[0:0], data...) - return nil -} diff --git a/native/request/raw_json_test.go b/native/request/raw_json_test.go deleted file mode 100644 index b5d1826..0000000 --- a/native/request/raw_json_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package request_test - -import ( - "encoding/json" - - . "github.com/mxmCherry/openrtb/native/response" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = Describe("RawJSON", func() { - - var _ json.Marshaler = (RawJSON)(nil) - var _ json.Unmarshaler = (*RawJSON)(nil) - - It("should encode JSON", func() { - subject := RawJSON(`true`) - - actual, err := subject.MarshalJSON() - Expect(err).NotTo(HaveOccurred()) - Expect(actual).To(Equal([]byte(`true`))) - }) - - It("should decode JSON", func() { - subject := RawJSON(nil) - - err := subject.UnmarshalJSON([]byte(`true`)) - Expect(err).NotTo(HaveOccurred()) - Expect(subject).To(Equal(RawJSON(`true`))) - }) - - It("should decode JSON when embedded into struct", func() { - wrapper := struct { - Raw RawJSON `json:"raw"` - }{ - Raw: nil, - } - - err := json.Unmarshal([]byte(`{"raw":true}`), &wrapper) - Expect(err).NotTo(HaveOccurred()) - Expect(wrapper.Raw).To(Equal(RawJSON(`true`))) - }) - - It("should encode JSON when embedded into struct", func() { - wrapper := struct { - Raw RawJSON `json:"raw"` - }{ - Raw: RawJSON(`true`), - } - - actual, err := json.Marshal(wrapper) - Expect(err).NotTo(HaveOccurred()) - Expect(actual).To(MatchJSON(`{"raw":true}`)) - }) - -}) diff --git a/native/request/request.go b/native/request/request.go index 2058218..1b68f4c 100644 --- a/native/request/request.go +++ b/native/request/request.go @@ -4,7 +4,11 @@ // https://www.iab.com/wp-content/uploads/2018/03/OpenRTB-Native-Ads-Specification-Final-1.2.pdf package request -import "github.com/mxmCherry/openrtb/native" +import ( + "encoding/json" + + "github.com/mxmCherry/openrtb/native" +) // 4.1 Native Markup Request Object // @@ -179,5 +183,5 @@ type Request struct { // object // Description: // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/request/title.go b/native/request/title.go index 1824512..a7e49ff 100644 --- a/native/request/title.go +++ b/native/request/title.go @@ -1,5 +1,7 @@ package request +import "encoding/json" + // 4.3 Title Object // // The Title object is to be used for title element of the Native ad. @@ -23,5 +25,5 @@ type Title struct { // object // Description: // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/request/video.go b/native/request/video.go index c1b0142..0eaa23a 100644 --- a/native/request/video.go +++ b/native/request/video.go @@ -1,5 +1,7 @@ package request +import "encoding/json" + // 4.5 Video Object // // The video object to be used for all video elements supported in the Native Ad. @@ -59,5 +61,5 @@ type Video struct { // object // Description: // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/response/asset.go b/native/response/asset.go index 5f4754d..692b7fd 100644 --- a/native/response/asset.go +++ b/native/response/asset.go @@ -1,5 +1,7 @@ package response +import "encoding/json" + // 5.2 Object: Asset // // Corresponds to the Asset Object in the request. The main container object for each asset @@ -97,5 +99,5 @@ type Asset struct { // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification // Bidders are encouraged not to use asset.ext for exchanging text assets. // Use data.ext with custom type instead. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/response/data.go b/native/response/data.go index 460e8ef..bf92bb5 100644 --- a/native/response/data.go +++ b/native/response/data.go @@ -1,6 +1,10 @@ package response -import "github.com/mxmCherry/openrtb/native" +import ( + "encoding/json" + + "github.com/mxmCherry/openrtb/native" +) // 5.5 Object: Data // @@ -60,5 +64,5 @@ type Data struct { // object // Description: // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/response/event_tracker.go b/native/response/event_tracker.go index be0d488..53d0542 100644 --- a/native/response/event_tracker.go +++ b/native/response/event_tracker.go @@ -1,6 +1,10 @@ package response -import "github.com/mxmCherry/openrtb/native" +import ( + "encoding/json" + + "github.com/mxmCherry/openrtb/native" +) // 5.8 Event Tracker Response Object // @@ -53,7 +57,7 @@ type EventTracker struct { // Dev note: // - type is a key-value object (not specified if value is string-only) // - descriptions says "an array of key:value objects" (???) - CustomData RawJSON `json:"customdata,omitempty"` + CustomData json.RawMessage `json:"customdata,omitempty"` // Field: // ext @@ -63,5 +67,5 @@ type EventTracker struct { // object // Description: // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/response/image.go b/native/response/image.go index 83f1dd0..bd96ce2 100644 --- a/native/response/image.go +++ b/native/response/image.go @@ -1,6 +1,10 @@ package response -import "github.com/mxmCherry/openrtb/native" +import ( + "encoding/json" + + "github.com/mxmCherry/openrtb/native" +) // 5.4 Object: Image // @@ -62,5 +66,5 @@ type Image struct { // object // Description: // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/response/link.go b/native/response/link.go index 9f78b66..ba966c0 100644 --- a/native/response/link.go +++ b/native/response/link.go @@ -1,5 +1,7 @@ package response +import "encoding/json" + // 5.7 Object: Link // // Used for ‘call to action’ assets, or other links from the Native ad. @@ -45,5 +47,5 @@ type Link struct { // object // Description: // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/response/raw_json.go b/native/response/raw_json.go deleted file mode 100644 index 8806ee2..0000000 --- a/native/response/raw_json.go +++ /dev/null @@ -1,27 +0,0 @@ -package response - -import "errors" - -// RawJSON is a raw encoded JSON value. -// It implements encoding/json.Marshaler and encoding/json.Unmarshaler and can -// be used to delay JSON decoding or precompute a JSON encoding. -// -// Basically, it's just a copy of encoding/json.RawMessage type, -// but with more convenient non-pointer encoding. -// -// HEADS UP: this will be replaced with json.RawMessage when Go 1.10 is out. -type RawJSON []byte - -// MarshalJSON returns m as the JSON encoding of m. -func (m RawJSON) MarshalJSON() ([]byte, error) { - return m, nil -} - -// UnmarshalJSON sets *m to a copy of data. -func (m *RawJSON) UnmarshalJSON(data []byte) error { - if m == nil { - return errors.New("openrtb/native/response.RawJSON: UnmarshalJSON on nil pointer") - } - *m = append((*m)[0:0], data...) - return nil -} diff --git a/native/response/raw_json_test.go b/native/response/raw_json_test.go deleted file mode 100644 index 735f62d..0000000 --- a/native/response/raw_json_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package response_test - -import ( - "encoding/json" - - . "github.com/mxmCherry/openrtb/native/response" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = Describe("RawJSON", func() { - - var _ json.Marshaler = (RawJSON)(nil) - var _ json.Unmarshaler = (*RawJSON)(nil) - - It("should encode JSON", func() { - subject := RawJSON(`true`) - - actual, err := subject.MarshalJSON() - Expect(err).NotTo(HaveOccurred()) - Expect(actual).To(Equal([]byte(`true`))) - }) - - It("should decode JSON", func() { - subject := RawJSON(nil) - - err := subject.UnmarshalJSON([]byte(`true`)) - Expect(err).NotTo(HaveOccurred()) - Expect(subject).To(Equal(RawJSON(`true`))) - }) - - It("should decode JSON when embedded into struct", func() { - wrapper := struct { - Raw RawJSON `json:"raw"` - }{ - Raw: nil, - } - - err := json.Unmarshal([]byte(`{"raw":true}`), &wrapper) - Expect(err).NotTo(HaveOccurred()) - Expect(wrapper.Raw).To(Equal(RawJSON(`true`))) - }) - - It("should encode JSON when embedded into struct", func() { - wrapper := struct { - Raw RawJSON `json:"raw"` - }{ - Raw: RawJSON(`true`), - } - - actual, err := json.Marshal(wrapper) - Expect(err).NotTo(HaveOccurred()) - Expect(actual).To(MatchJSON(`{"raw":true}`)) - }) - -}) diff --git a/native/response/response.go b/native/response/response.go index e81fdde..69cb927 100644 --- a/native/response/response.go +++ b/native/response/response.go @@ -4,6 +4,8 @@ // https://www.iab.com/wp-content/uploads/2018/03/OpenRTB-Native-Ads-Specification-Final-1.2.pdf package response +import "encoding/json" + // 5.1 Object: Response // // The native object is the top level JSON object which identifies a native response. @@ -138,5 +140,5 @@ type Response struct { // object // Description: // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/native/response/title.go b/native/response/title.go index a7f9faa..cdf1cb1 100644 --- a/native/response/title.go +++ b/native/response/title.go @@ -1,5 +1,7 @@ package response +import "encoding/json" + // 5.3 Object: Title // // Corresponds to the Title Object in the request, with the value filled in. @@ -35,5 +37,5 @@ type Title struct { // object // Description: // This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/pmp.go b/pmp.go index cdb9f63..24d43d3 100644 --- a/pmp.go +++ b/pmp.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.11 Object: Pmp // // This object is the private marketplace container for direct deals between buyers and sellers that may pertain to this impression. @@ -32,5 +34,5 @@ type PMP struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/producer.go b/producer.go index eabdbc3..c03f961 100644 --- a/producer.go +++ b/producer.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.17 Object: Producer // // This object defines the producer of the content in which the ad will be shown. @@ -47,5 +49,5 @@ type Producer struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/publisher.go b/publisher.go index a92a3dd..51b2fa5 100644 --- a/publisher.go +++ b/publisher.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.15 Object: Publisher // // This object describes the publisher of the media in which the ad will be displayed. @@ -45,5 +47,5 @@ type Publisher struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/raw_json.go b/raw_json.go deleted file mode 100644 index 86c12ac..0000000 --- a/raw_json.go +++ /dev/null @@ -1,27 +0,0 @@ -package openrtb - -import "errors" - -// RawJSON is a raw encoded JSON value. -// It implements encoding/json.Marshaler and encoding/json.Unmarshaler and can -// be used to delay JSON decoding or precompute a JSON encoding. -// -// Basically, it's just a copy of encoding/json.RawMessage type, -// but with more convenient non-pointer encoding. -// -// HEADS UP: this will be replaced with json.RawMessage when Go 1.10 is out. -type RawJSON []byte - -// MarshalJSON returns m as the JSON encoding of m. -func (m RawJSON) MarshalJSON() ([]byte, error) { - return m, nil -} - -// UnmarshalJSON sets *m to a copy of data. -func (m *RawJSON) UnmarshalJSON(data []byte) error { - if m == nil { - return errors.New("openrtb.RawJSON: UnmarshalJSON on nil pointer") - } - *m = append((*m)[0:0], data...) - return nil -} diff --git a/raw_json_test.go b/raw_json_test.go deleted file mode 100644 index 4515e58..0000000 --- a/raw_json_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package openrtb_test - -import ( - "encoding/json" - - . "github.com/mxmCherry/openrtb" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = Describe("RawJSON", func() { - - var _ json.Marshaler = (RawJSON)(nil) - var _ json.Unmarshaler = (*RawJSON)(nil) - - It("should encode JSON", func() { - subject := RawJSON(`true`) - - actual, err := subject.MarshalJSON() - Expect(err).NotTo(HaveOccurred()) - Expect(actual).To(Equal([]byte(`true`))) - }) - - It("should decode JSON", func() { - subject := RawJSON(nil) - - err := subject.UnmarshalJSON([]byte(`true`)) - Expect(err).NotTo(HaveOccurred()) - Expect(subject).To(Equal(RawJSON(`true`))) - }) - - It("should decode JSON when embedded into struct", func() { - wrapper := struct { - Raw RawJSON `json:"raw"` - }{ - Raw: nil, - } - - err := json.Unmarshal([]byte(`{"raw":true}`), &wrapper) - Expect(err).NotTo(HaveOccurred()) - Expect(wrapper.Raw).To(Equal(RawJSON(`true`))) - }) - - It("should encode JSON when embedded into struct", func() { - wrapper := struct { - Raw RawJSON `json:"raw"` - }{ - Raw: RawJSON(`true`), - } - - actual, err := json.Marshal(wrapper) - Expect(err).NotTo(HaveOccurred()) - Expect(actual).To(MatchJSON(`{"raw":true}`)) - }) - -}) diff --git a/regs.go b/regs.go index be1acf8..6413859 100644 --- a/regs.go +++ b/regs.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.3 Object: Regs // // This object contains any legal, governmental, or industry regulations that apply to the request. @@ -22,5 +24,5 @@ type Regs struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/seat_bid.go b/seat_bid.go index 88331ae..e01f700 100644 --- a/seat_bid.go +++ b/seat_bid.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 4.2.2 Object: SeatBid // // A bid response can contain multiple SeatBid objects, each on behalf of a different bidder seat and each containing one or more individual bids. @@ -39,5 +41,5 @@ type SeatBid struct { // object // Description: // Placeholder for bidder-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/segment.go b/segment.go index f1a2080..d5c4d07 100644 --- a/segment.go +++ b/segment.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.22 Object: Segment // // Segment objects are essentially key-value pairs that convey specific units of data. @@ -37,5 +39,5 @@ type Segment struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/site.go b/site.go index 67f7594..dcbeab3 100644 --- a/site.go +++ b/site.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.13 Object: Site // // This object should be included if the ad supported content is a website as opposed to a non-browser application. @@ -128,5 +130,5 @@ type Site struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/source.go b/source.go index 89211e7..f341d44 100644 --- a/source.go +++ b/source.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.2 Object: Source // // This object describes the nature and behavior of the entity that is the source of the bid request upstream from the exchange. @@ -40,5 +42,5 @@ type Source struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/user.go b/user.go index 77f9dce..ad1783f 100644 --- a/user.go +++ b/user.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.20 Object: User // // This object contains information known or derived about the human user of the device (i.e., the audience for advertising). @@ -85,5 +87,5 @@ type User struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` } diff --git a/video.go b/video.go index 347dde5..d7d7d4e 100644 --- a/video.go +++ b/video.go @@ -1,5 +1,7 @@ package openrtb +import "encoding/json" + // 3.2.7 Object: Video // // This object represents an in-stream video impression. @@ -261,5 +263,5 @@ type Video struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext RawJSON `json:"ext,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` }