Best Ginkgo code snippet using types.UnmarshalJSON
activity.go
Source:activity.go
...49 Data json.RawMessage `json:"data"`50 } `json:"actor"`51 } `json:"relationships"`52}53// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.54func (a *Activity) UnmarshalJSON(b []byte) error {55 var helper activityUnmarshalHelper56 helper.Attributes = &helper.activity57 if err := json.Unmarshal(b, &helper); err != nil {58 return err59 }60 *a = Activity(helper.activity)61 a.Attachments = helper.Relationships.Attachments.Data62 a.RawActor = helper.Relationships.RawActor.Data63 a.rawData = b64 return nil65}66// Actor returns returns the parsed actor. For recognized actor types, a value of the corresponding struct type will be returned.67func (a *Activity) Actor() (actor interface{}) {68 var obj unknownResource69 if err := json.Unmarshal(a.RawActor, &obj); err != nil {70 panic(err.Error())71 }72 switch *obj.Type {73 case UserType:74 actor = &User{}75 case ProgramType:76 actor = &Program{}77 }78 if err := json.Unmarshal(a.RawActor, &actor); err != nil {79 panic(err.Error())80 }81 return actor82}83// Activity returns the parsed activity. For recognized activity types, a value of the corresponding struct type will be returned.84func (a *Activity) Activity() (activity interface{}) {85 switch *a.Type {86 case ActivityBountyAwardedType:87 activity = &ActivityBountyAwarded{}88 case ActivityBountySuggestedType:89 activity = &ActivityBountySuggested{}90 case ActivityBugClonedType:91 activity = &ActivityBugCloned{}92 case ActivityExternalUserInvitationCancelledType:93 activity = &ActivityExternalUserInvitationCancelled{}94 case ActivityExternalUserInvitedType:95 activity = &ActivityExternalUserInvited{}96 case ActivityExternalUserJoinedType:97 activity = &ActivityExternalUserJoined{}98 case ActivityExternalUserRemovedType:99 activity = &ActivityExternalUserRemoved{}100 case ActivityGroupAssignedToBugType:101 activity = &ActivityGroupAssignedToBug{}102 case ActivityReferenceIDAddedType:103 activity = &ActivityReferenceIDAdded{}104 case ActivityReportTitleUpdatedType:105 activity = &ActivityReportTitleUpdated{}106 case ActivityReportVulnerabilityTypesUpdatedType:107 activity = &ActivityReportVulnerabilityTypesUpdated{}108 case ActivitySwagAwardedType:109 activity = &ActivitySwagAwarded{}110 case ActivityUserAssignedToBugType:111 activity = &ActivityUserAssignedToBug{}112 case ActivityUserBannedFromProgramType:113 activity = &ActivityUserBannedFromProgram{}114 default:115 return nil116 }117 if err := json.Unmarshal(a.rawData, activity); err != nil {118 panic(err.Error())119 }120 return activity121}122// Report returns the report this activity is a child of123func (a *Activity) Report() *Report {124 return a.report125}126// ActivityBountyAwarded occurs when a bounty is awarded.127//128// HackerOne API docs:https://api.hackerone.com/docs/v1#activity-bounty-awarded129type ActivityBountyAwarded struct {130 BountyAmount *string `json:"bounty_amount"`131 BonusAmount *string `json:"bonus_amount"`132}133// Helper types for JSONUnmarshal134type activityBountyAwarded ActivityBountyAwarded // Used to avoid recursion of JSONUnmarshal135type activityBountyAwardedUnmarshalHelper struct {136 Attributes activityBountyAwarded `json:"attributes"`137}138// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.139func (a *ActivityBountyAwarded) UnmarshalJSON(b []byte) error {140 var helper activityBountyAwardedUnmarshalHelper141 if err := json.Unmarshal(b, &helper); err != nil {142 return err143 }144 *a = ActivityBountyAwarded(helper.Attributes)145 return nil146}147// ActivityBountySuggested occurs when a bounty is suggested.148//149// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-bounty-suggested150type ActivityBountySuggested struct {151 BountyAmount *string `json:"bounty_amount"`152 BonusAmount *string `json:"bonus_amount"`153}154// Helper types for JSONUnmarshal155type activityBountySuggested ActivityBountySuggested // Used to avoid recursion of JSONUnmarshal156type activityBountySuggestedUnmarshalHelper struct {157 Attributes activityBountySuggested `json:"attributes"`158}159// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.160func (a *ActivityBountySuggested) UnmarshalJSON(b []byte) error {161 var helper activityBountySuggestedUnmarshalHelper162 if err := json.Unmarshal(b, &helper); err != nil {163 return err164 }165 *a = ActivityBountySuggested(helper.Attributes)166 return nil167}168// ActivityBugCloned occurs when a bug is cloned.169//170// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-bug-cloned171type ActivityBugCloned struct {172 OriginalReportID *int `json:"original_report_id"`173}174// Helper types for JSONUnmarshal175type activityBugCloned ActivityBugCloned // Used to avoid recursion of JSONUnmarshal176type activityBugClonedUnmarshalHelper struct {177 Attributes activityBugCloned `json:"attributes"`178}179// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.180func (a *ActivityBugCloned) UnmarshalJSON(b []byte) error {181 var helper activityBugClonedUnmarshalHelper182 if err := json.Unmarshal(b, &helper); err != nil {183 return err184 }185 *a = ActivityBugCloned(helper.Attributes)186 return nil187}188// ActivityExternalUserInvitationCancelled occurs when a external user's invitiation is cancelled.189//190// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-external-user-invitation-cancelled191type ActivityExternalUserInvitationCancelled struct {192 Email *string `json:"email"`193}194// Helper types for JSONUnmarshal195type activityExternalUserInvitationCancelled ActivityExternalUserInvitationCancelled // Used to avoid recursion of JSONUnmarshal196type activityExternalUserInvitationCancelledSuggestedUnmarshalHelper struct {197 Attributes activityExternalUserInvitationCancelled `json:"attributes"`198}199// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.200func (a *ActivityExternalUserInvitationCancelled) UnmarshalJSON(b []byte) error {201 var helper activityExternalUserInvitationCancelledSuggestedUnmarshalHelper202 if err := json.Unmarshal(b, &helper); err != nil {203 return err204 }205 *a = ActivityExternalUserInvitationCancelled(helper.Attributes)206 return nil207}208// ActivityExternalUserInvited occurs when a external user is invited.209//210// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-external-user-invited211type ActivityExternalUserInvited struct {212 Email *string `json:"email"`213}214// Helper types for JSONUnmarshal215type activityExternalUserInvited ActivityExternalUserInvited // Used to avoid recursion of JSONUnmarshal216type activityExternalUserInvitedUnmarshalHelper struct {217 Attributes activityExternalUserInvited `json:"attributes"`218}219// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.220func (a *ActivityExternalUserInvited) UnmarshalJSON(b []byte) error {221 var helper activityExternalUserInvitedUnmarshalHelper222 if err := json.Unmarshal(b, &helper); err != nil {223 return err224 }225 *a = ActivityExternalUserInvited(helper.Attributes)226 return nil227}228// ActivityExternalUserJoined occurs when a external user joins.229//230// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-external-user-joined231type ActivityExternalUserJoined struct {232 DuplicateReportID *int `json:"duplicate_report_id"`233}234// Helper types for JSONUnmarshal235type activityExternalUserJoined ActivityExternalUserJoined // Used to avoid recursion of JSONUnmarshal236type activityExternalUserJoinedUnmarshalHelper struct {237 Attributes activityExternalUserJoined `json:"attributes"`238}239// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.240func (a *ActivityExternalUserJoined) UnmarshalJSON(b []byte) error {241 var helper activityExternalUserJoinedUnmarshalHelper242 if err := json.Unmarshal(b, &helper); err != nil {243 return err244 }245 *a = ActivityExternalUserJoined(helper.Attributes)246 return nil247}248// ActivityExternalUserRemoved occurs when a external user is removed249//250// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-external-user-removed251type ActivityExternalUserRemoved struct {252 RemovedUser *User `json:"removed_user"`253}254// Helper types for JSONUnmarshal255type activityExternalUserRemoved ActivityExternalUserRemoved // Used to avoid recursion of JSONUnmarshal256type activityExternalUserRemovedUnmarshalHelper struct {257 Relationships struct {258 RemovedUser struct {259 Data *User `json:"data"`260 } `json:"removed_user"`261 } `json:"relationships"`262}263// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.264func (a *ActivityExternalUserRemoved) UnmarshalJSON(b []byte) error {265 var helper activityExternalUserRemovedUnmarshalHelper266 if err := json.Unmarshal(b, &helper); err != nil {267 return err268 }269 a.RemovedUser = helper.Relationships.RemovedUser.Data270 return nil271}272// ActivityGroupAssignedToBug occurs when a group is assigned to a report.273//274// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-group-assigned-to-bug275type ActivityGroupAssignedToBug struct {276 Group *Group `json:"group"`277}278// Helper types for JSONUnmarshal279type activityGroupAssignedToBug ActivityGroupAssignedToBug // Used to avoid recursion of JSONUnmarshal280type activityGroupAssignedToBugUnmarshalHelper struct {281 Relationships struct {282 Group struct {283 Data *Group `json:"data"`284 } `json:"group"`285 } `json:"relationships"`286}287// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.288func (a *ActivityGroupAssignedToBug) UnmarshalJSON(b []byte) error {289 var helper activityGroupAssignedToBugUnmarshalHelper290 if err := json.Unmarshal(b, &helper); err != nil {291 return err292 }293 a.Group = helper.Relationships.Group.Data294 return nil295}296// ActivityReferenceIDAdded occurs when a reference id/url is added to a report.297//298// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-reference-id-added299type ActivityReferenceIDAdded struct {300 Reference *string `json:"reference"`301 ReferenceURL *string `json:"reference_url"`302}303// Helper types for JSONUnmarshal304type activityReferenceIDAdded ActivityReferenceIDAdded // Used to avoid recursion of JSONUnmarshal305type activityReferenceIDAddedUnmarshalHelper struct {306 Attributes activityReferenceIDAdded `json:"attributes"`307}308// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.309func (a *ActivityReferenceIDAdded) UnmarshalJSON(b []byte) error {310 var helper activityReferenceIDAddedUnmarshalHelper311 if err := json.Unmarshal(b, &helper); err != nil {312 return err313 }314 *a = ActivityReferenceIDAdded(helper.Attributes)315 return nil316}317// ActivityReportTitleUpdated occurs when report title is updated318//319// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-report-title-updated320type ActivityReportTitleUpdated struct {321 OldTitle *string `json:"old_title"`322 NewTitle *string `json:"new_title"`323}324// Helper types for JSONUnmarshal325type activityReportTitleUpdated ActivityReportTitleUpdated // Used to avoid recursion of JSONUnmarshal326type activityReportTitleUpdatedUnmarshalHelper struct {327 Attributes activityReportTitleUpdated `json:"attributes"`328}329// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.330func (a *ActivityReportTitleUpdated) UnmarshalJSON(b []byte) error {331 var helper activityReportTitleUpdatedUnmarshalHelper332 if err := json.Unmarshal(b, &helper); err != nil {333 return err334 }335 *a = ActivityReportTitleUpdated(helper.Attributes)336 return nil337}338// ActivityReportVulnerabilityTypesUpdated occurs when vulnerability types for a report are updated.339//340// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-report-vulnerability-types-updated341type ActivityReportVulnerabilityTypesUpdated struct {342 OldVulnerabilityTypes []VulnerabilityType `json:"old_vulnerability_types"`343 NewVulnerabilityTypes []VulnerabilityType `json:"new_vulnerability_types"`344}345// Helper types for JSONUnmarshal346type activityReportVulnerabilityTypesUpdated ActivityReportVulnerabilityTypesUpdated // Used to avoid recursion of JSONUnmarshal347type activityReportVulnerabilityTypesUpdatedUnmarshalHelper struct {348 Relationships struct {349 OldVulnerabilityTypes struct {350 Data []VulnerabilityType `json:"data"`351 } `json:"old_vulnerability_types"`352 NewVulnerabilityTypes struct {353 Data []VulnerabilityType `json:"data"`354 } `json:"new_vulnerability_types"`355 } `json:"relationships"`356}357// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.358func (a *ActivityReportVulnerabilityTypesUpdated) UnmarshalJSON(b []byte) error {359 var helper activityReportVulnerabilityTypesUpdatedUnmarshalHelper360 if err := json.Unmarshal(b, &helper); err != nil {361 return err362 }363 a.OldVulnerabilityTypes = helper.Relationships.OldVulnerabilityTypes.Data364 a.NewVulnerabilityTypes = helper.Relationships.NewVulnerabilityTypes.Data365 return nil366}367// ActivitySwagAwarded occurs when swag is awarded368//369// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-swag-awarded370type ActivitySwagAwarded struct {371 Swag *Swag `json:"swag"`372}373// Helper types for JSONUnmarshal374type activitySwagAwarded ActivitySwagAwarded // Used to avoid recursion of JSONUnmarshal375type activitySwagAwardedUnmarshalHelper struct {376 Relationships struct {377 Swag struct {378 Data *Swag `json:"data"`379 } `json:"swag"`380 } `json:"relationships"`381}382// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.383func (a *ActivitySwagAwarded) UnmarshalJSON(b []byte) error {384 var helper activitySwagAwardedUnmarshalHelper385 if err := json.Unmarshal(b, &helper); err != nil {386 return err387 }388 a.Swag = helper.Relationships.Swag.Data389 return nil390}391// ActivityUserAssignedToBug occurs when a user is assigned to a report.392//393// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-user-assigned-to-bug394type ActivityUserAssignedToBug struct {395 AssignedUser *User `json:"assigned_user"`396}397// Helper types for JSONUnmarshal398type activityUserAssignedToBug ActivityUserAssignedToBug // Used to avoid recursion of JSONUnmarshal399type activityUserAssignedToBugUnmarshalHelper struct {400 Relationships struct {401 AssignedUser struct {402 Data *User `json:"data"`403 } `json:"assigned_user"`404 } `json:"relationships"`405}406// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.407func (a *ActivityUserAssignedToBug) UnmarshalJSON(b []byte) error {408 var helper activityUserAssignedToBugUnmarshalHelper409 if err := json.Unmarshal(b, &helper); err != nil {410 return err411 }412 a.AssignedUser = helper.Relationships.AssignedUser.Data413 return nil414}415// ActivityUserBannedFromProgram occurs when a user is banned from a program.416//417// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-user-banned-from-program418type ActivityUserBannedFromProgram struct {419 RemovedUser *User `json:"removed_user"`420}421// Helper types for JSONUnmarshal422type activityUserBannedFromProgram ActivityUserBannedFromProgram // Used to avoid recursion of JSONUnmarshal423type activityUserBannedFromProgramUnmarshalHelper struct {424 Relationships struct {425 RemovedUser struct {426 Data *User `json:"data"`427 } `json:"removed_user"`428 } `json:"relationships"`429}430// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.431func (a *ActivityUserBannedFromProgram) UnmarshalJSON(b []byte) error {432 var helper activityUserBannedFromProgramUnmarshalHelper433 if err := json.Unmarshal(b, &helper); err != nil {434 return err435 }436 a.RemovedUser = helper.Relationships.RemovedUser.Data437 return nil438}...
encoding.go
Source:encoding.go
...29 EncDurationInTraffic *internal.Duration `json:"duration_in_traffic"`30 EncArrivalTime *internal.DateTime `json:"arrival_time"`31 EncDepartureTime *internal.DateTime `json:"departure_time"`32}33// UnmarshalJSON implements json.Unmarshaler for Leg. This decodes the API34// representation into types useful for Go developers.35func (leg *Leg) UnmarshalJSON(data []byte) error {36 x := encodedLeg{}37 err := json.Unmarshal(data, &x)38 if err != nil {39 return err40 }41 *leg = Leg(x.safeLeg)42 leg.Duration = x.EncDuration.Duration()43 leg.DurationInTraffic = x.EncDurationInTraffic.Duration()44 leg.ArrivalTime = x.EncArrivalTime.Time()45 leg.DepartureTime = x.EncDepartureTime.Time()46 return nil47}48// MarshalJSON implements json.Marshaler for Leg. This encodes Go types back to49// the API representation.50func (leg *Leg) MarshalJSON() ([]byte, error) {51 x := encodedLeg{}52 x.safeLeg = safeLeg(*leg)53 x.EncDuration = internal.NewDuration(leg.Duration)54 x.EncDurationInTraffic = internal.NewDuration(leg.DurationInTraffic)55 x.EncArrivalTime = internal.NewDateTime(leg.ArrivalTime)56 x.EncDepartureTime = internal.NewDateTime(leg.DepartureTime)57 return json.Marshal(x)58}59// safeStep is a raw version of Step that does not have custom encoding or60// decoding methods applied.61type safeStep Step62// encodedStep is the actual encoded version of Step as per the Maps APIs.63type encodedStep struct {64 safeStep65 EncDuration *internal.Duration `json:"duration"`66}67// UnmarshalJSON implements json.Unmarshaler for Step. This decodes the API68// representation into types useful for Go developers.69func (step *Step) UnmarshalJSON(data []byte) error {70 x := encodedStep{}71 err := json.Unmarshal(data, &x)72 if err != nil {73 return err74 }75 *step = Step(x.safeStep)76 step.Duration = x.EncDuration.Duration()77 return nil78}79// MarshalJSON implements json.Marshaler for Step. This encodes Go types back to80// the API representation.81func (step *Step) MarshalJSON() ([]byte, error) {82 x := encodedStep{}83 x.safeStep = safeStep(*step)84 x.EncDuration = internal.NewDuration(step.Duration)85 return json.Marshal(x)86}87// safeTransitDetails is a raw version of TransitDetails that does not have88// custom encoding or decoding methods applied.89type safeTransitDetails TransitDetails90// encodedTransitDetails is the actual encoded version of TransitDetails as per91// the Maps APIs92type encodedTransitDetails struct {93 safeTransitDetails94 EncArrivalTime *internal.DateTime `json:"arrival_time"`95 EncDepartureTime *internal.DateTime `json:"departure_time"`96}97// UnmarshalJSON implements json.Unmarshaler for TransitDetails. This decodes98// the API representation into types useful for Go developers.99func (transitDetails *TransitDetails) UnmarshalJSON(data []byte) error {100 x := encodedTransitDetails{}101 err := json.Unmarshal(data, &x)102 if err != nil {103 return err104 }105 *transitDetails = TransitDetails(x.safeTransitDetails)106 transitDetails.ArrivalTime = x.EncArrivalTime.Time()107 transitDetails.DepartureTime = x.EncDepartureTime.Time()108 return nil109}110// MarshalJSON implements json.Marshaler for TransitDetails. This encodes Go111// types back to the API representation.112func (transitDetails *TransitDetails) MarshalJSON() ([]byte, error) {113 x := encodedTransitDetails{}114 x.safeTransitDetails = safeTransitDetails(*transitDetails)115 x.EncArrivalTime = internal.NewDateTime(transitDetails.ArrivalTime)116 x.EncDepartureTime = internal.NewDateTime(transitDetails.DepartureTime)117 return json.Marshal(x)118}119// safeTransitLine is the raw version of TransitLine that does not have custom120// encoding or decoding methods applied.121type safeTransitLine TransitLine122// encodedTransitLine is the actual encoded version of TransitLine as per the123// Maps APIs124type encodedTransitLine struct {125 safeTransitLine126 EncURL string `json:"url"`127 EncIcon string `json:"icon"`128}129// UnmarshalJSON imlpements json.Unmarshaler for TransitLine. This decodes the130// API representation into types useful for Go developers.131func (transitLine *TransitLine) UnmarshalJSON(data []byte) error {132 x := encodedTransitLine{}133 err := json.Unmarshal(data, &x)134 if err != nil {135 return err136 }137 *transitLine = TransitLine(x.safeTransitLine)138 transitLine.URL, err = url.Parse(x.EncURL)139 if err != nil {140 return err141 }142 transitLine.Icon, err = url.Parse(x.EncIcon)143 if err != nil {144 return err145 }146 return nil147}148// MarshalJSON implements json.Marshaler for TransitLine. This encodes Go149// types back to the API representation.150func (transitLine *TransitLine) MarshalJSON() ([]byte, error) {151 x := encodedTransitLine{}152 x.safeTransitLine = safeTransitLine(*transitLine)153 x.EncURL = transitLine.URL.String()154 x.EncIcon = transitLine.Icon.String()155 return json.Marshal(x)156}157// safeTransitAgency is the raw version of TransitAgency that does not have158// custom encoding or decoding methods applied.159type safeTransitAgency TransitAgency160// encodedTransitAgency is the actual encoded version of TransitAgency as per the161// Maps APIs162type encodedTransitAgency struct {163 safeTransitAgency164 EncURL string `json:"url"`165}166// UnmarshalJSON imlpements json.Unmarshaler for TransitAgency. This decodes the167// API representation into types useful for Go developers.168func (transitAgency *TransitAgency) UnmarshalJSON(data []byte) error {169 x := encodedTransitAgency{}170 err := json.Unmarshal(data, &x)171 if err != nil {172 return err173 }174 *transitAgency = TransitAgency(x.safeTransitAgency)175 transitAgency.URL, err = url.Parse(x.EncURL)176 if err != nil {177 return err178 }179 return nil180}181// MarshalJSON implements json.Marshaler for TransitAgency. This encodes Go182// types back to the API representation.183func (transitAgency *TransitAgency) MarshalJSON() ([]byte, error) {184 x := encodedTransitAgency{}185 x.safeTransitAgency = safeTransitAgency(*transitAgency)186 x.EncURL = transitAgency.URL.String()187 return json.Marshal(x)188}189// safeTransitLineVehicle is the raw version of TransitLineVehicle that does not190// have custom encoding or decoding methods applied.191type safeTransitLineVehicle TransitLineVehicle192// encodedTransitLineVehicle is the actual encoded version of TransitLineVehicle193// as per the Maps APIs194type encodedTransitLineVehicle struct {195 safeTransitLineVehicle196 EncIcon string `json:"icon"`197}198// UnmarshalJSON imlpements json.Unmarshaler for TransitLineVehicle. This199// decodes the API representation into types useful for Go developers.200func (transitLineVehicle *TransitLineVehicle) UnmarshalJSON(data []byte) error {201 x := encodedTransitLineVehicle{}202 err := json.Unmarshal(data, &x)203 if err != nil {204 return err205 }206 *transitLineVehicle = TransitLineVehicle(x.safeTransitLineVehicle)207 transitLineVehicle.Icon, err = url.Parse(x.EncIcon)208 if err != nil {209 return err210 }211 return nil212}213// MarshalJSON implements json.Marshaler for TransitLineVehicle. This encodes214// Go types back to the API representation.215func (transitLineVehicle *TransitLineVehicle) MarshalJSON() ([]byte, error) {216 x := encodedTransitLineVehicle{}217 x.safeTransitLineVehicle = safeTransitLineVehicle(*transitLineVehicle)218 x.EncIcon = transitLineVehicle.Icon.String()219 return json.Marshal(x)220}221// safeDistanceMatrixElement is a raw version of DistanceMatrixElement that222// does not have custom encoding or decoding methods applied.223type safeDistanceMatrixElement DistanceMatrixElement224// encodedDistanceMatrixElement is the actual encoded version of225// DistanceMatrixElement as per the Maps APIs.226type encodedDistanceMatrixElement struct {227 safeDistanceMatrixElement228 EncDuration *internal.Duration `json:"duration"`229 EncDurationInTraffic *internal.Duration `json:"duration_in_traffic"`230}231// UnmarshalJSON implements json.Unmarshaler for DistanceMatrixElement. This232// decodes the API representation into types useful for Go developers.233func (dme *DistanceMatrixElement) UnmarshalJSON(data []byte) error {234 x := encodedDistanceMatrixElement{}235 err := json.Unmarshal(data, &x)236 if err != nil {237 return err238 }239 *dme = DistanceMatrixElement(x.safeDistanceMatrixElement)240 dme.Duration = x.EncDuration.Duration()241 dme.DurationInTraffic = x.EncDurationInTraffic.Duration()242 return nil243}244// MarshalJSON implements json.Marshaler for DistanceMatrixElement. This encodes245// Go types back to the API representation.246func (dme *DistanceMatrixElement) MarshalJSON() ([]byte, error) {247 x := encodedDistanceMatrixElement{}248 x.safeDistanceMatrixElement = safeDistanceMatrixElement(*dme)249 x.EncDuration = internal.NewDuration(dme.Duration)250 return json.Marshal(x)251}252// safeSnappedPoint is a raw version of SnappedPoint that does not have custom253// encoding or decoding methods applied.254type safeSnappedPoint SnappedPoint255// encodedSnappedPoint is the actual encoded version of SnappedPoint as per the256// Roads API.257type encodedSnappedPoint struct {258 safeSnappedPoint259 EncLocation internal.Location `json:"location"`260}261// UnmarshalJSON implements json.Unmarshaler for SnappedPoint. This decode the262// API representation into types useful for Go developers.263func (sp *SnappedPoint) UnmarshalJSON(data []byte) error {264 x := encodedSnappedPoint{}265 err := json.Unmarshal(data, &x)266 if err != nil {267 return err268 }269 *sp = SnappedPoint(x.safeSnappedPoint)270 sp.Location.Lat = x.EncLocation.Latitude271 sp.Location.Lng = x.EncLocation.Longitude272 return nil273}274// MarshalJSON implements json.Marshaler for SnappedPoint. This encodes Go275// types back to the API representation.276func (sp *SnappedPoint) MarshalJSON() ([]byte, error) {277 x := encodedSnappedPoint{}...
types_test.go
Source:types_test.go
...4 "github.com/stretchr/testify/assert"5 "testing"6 "time"7)8func Test_Common_Types_CustomFloat64_UnmarshalJSONStringSuccess(t *testing.T) {9 c := CustomFloat64{}10 _ = c.UnmarshalJSON([]byte(`"10.10"`))11 assert.Equal(t, 10.10, c.Value())12}13func Test_Common_Types_CustomFloat64_UnmarshalJSONStringError(t *testing.T) {14 c := CustomFloat64{}15 err := c.UnmarshalJSON([]byte(`"qwerty"`))16 assert.Error(t, err)17 assert.Equal(t, "CustomFloat64@UnmarshalJSON: invalid character 'q' looking for beginning of value", err.Error())18}19func Test_Common_Types_CustomFloat64_UnmarshalJSONFloatSuccess(t *testing.T) {20 c := CustomFloat64{}21 _ = c.UnmarshalJSON([]byte(`10.10`))22 assert.Equal(t, 10.10, c.Value())23}24func Test_Common_Types_CustomFloat64_UnmarshalJSONFloatError(t *testing.T) {25 c := CustomFloat64{}26 err := c.UnmarshalJSON([]byte(`qwerty`))27 assert.Error(t, err)28 assert.Equal(t, "CustomFloat64@UnmarshalJSON: invalid character 'q' looking for beginning of value", err.Error())29}30func Test_Common_Types_CustomFloat64_MarshalJSONSuccess(t *testing.T) {31 c := CustomFloat64{}32 c.Float64 = 10.1033 result, err := c.MarshalJSON()34 expected, _ := json.Marshal(c.Float64)35 assert.Equal(t, expected, result)36 assert.Nil(t, err)37}38func Test_Common_Types_CustomFloat64_MarshalJSONEmpty(t *testing.T) {39 c := CustomFloat64{}40 result, err := c.MarshalJSON()41 expected, _ := json.Marshal(c.Float64)42 assert.Equal(t, expected, result)43 assert.Nil(t, err)44}45func Test_Common_Types_CustomTimestamp_UnmarshalJSONSuccess(t *testing.T) {46 c := CustomTimestamp{}47 format := "2006-01-02 15:04:05"48 str := `"2020-09-10 15:15:15"`49 _ = c.UnmarshalJSON([]byte(str))50 assert.Equal(t, "2020-09-10 15:15:15", c.Value().Format(format))51}52func Test_Common_Types_CustomTimestamp_UnmarshalJSONError(t *testing.T) {53 c := CustomTimestamp{}54 str := `123456`55 err := c.UnmarshalJSON([]byte(str))56 assert.Error(t, err)57 assert.Equal(t, "CustomTimestamp@UnmarshalJSON: json: cannot unmarshal number into Go value of type string", err.Error())58}59func Test_Common_Types_CustomTimestamp_UnmarshalJSONErrorTimeParse(t *testing.T) {60 c := CustomTimestamp{}61 str := `"2006/01/02"`62 err := c.UnmarshalJSON([]byte(str))63 assert.Error(t, err)64 assert.Equal(t, `CustomTimestamp@UnmarshalJSON ParseTime: parsing time "2006/01/02" as "2006-01-02 15:04:05": cannot parse "/01/02" as "-"`, err.Error())65}66func Test_Common_Types_CustomTimestamp_MarshalJSONSuccess(t *testing.T) {67 c := CustomTimestamp{}68 c.Timestamp = time.Date(2020, time.Month(1), 20, 0, 0, 0, 0, time.UTC)69 result, err := c.MarshalJSON()70 assert.Equal(t, []byte(`"2020-01-20 00:00:00"`), result)71 assert.Nil(t, err)72}73func Test_Common_Types_CustomTimestamp_MarshalJSONEmpty(t *testing.T) {74 c := CustomTimestamp{}75 result, err := c.MarshalJSON()76 assert.Equal(t, []byte(`""`), result)77 assert.Nil(t, err)78}79//func Test_Common_Types_CustomTimestamp_MarshalJSONError(t *testing.T) {80// c := CustomTimestamp{}81// result, err := c.MarshalJSON()82// assert.Equal(t, []byte(``), result)83// assert.Nil(t, err)84//}85func Test_Common_Types_CustomDate_UnmarshalJSONSuccess(t *testing.T) {86 c := CustomDate{}87 format := "2006-01-02"88 str := `"2020-09-10"`89 _ = c.UnmarshalJSON([]byte(str))90 assert.Equal(t, "2020-09-10", c.Value().Format(format))91}92func Test_Common_Types_CustomDate_UnmarshalJSONError(t *testing.T) {93 c := CustomDate{}94 str := `123456`95 err := c.UnmarshalJSON([]byte(str))96 assert.Error(t, err)97 assert.Equal(t, "CustomDate@UnmarshalJSON: json: cannot unmarshal number into Go value of type string", err.Error())98}99func Test_Common_Types_CustomDate_UnmarshalJSONErrorTimeParse(t *testing.T) {100 c := CustomDate{}101 str := `"2006/01/02"`102 err := c.UnmarshalJSON([]byte(str))103 assert.Error(t, err)104 assert.Equal(t, `CustomDate@UnmarshalJSON ParseTime: parsing time "2006/01/02" as "2006-01-02": cannot parse "/01/02" as "-"`, err.Error())105}106func Test_Common_Types_CustomDate_MarshalJSONSuccess(t *testing.T) {107 c := CustomDate{}108 c.Date = time.Date(2020, time.Month(1), 20, 0, 0, 0, 0, time.UTC)109 result, err := c.MarshalJSON()110 assert.Equal(t, []byte(`"2020-01-20"`), result)111 assert.Nil(t, err)112}113func Test_Common_Types_CustomDate_MarshalJSONEmpty(t *testing.T) {114 c := CustomDate{}115 result, err := c.MarshalJSON()116 assert.Equal(t, []byte(`""`), result)117 assert.Nil(t, err)118}...
UnmarshalJSON
Using AI Code Generation
1func main() {2 var jsonBlob = []byte(`[{"Name": "Platypus", "Order": "Monotremata"},{"Name": "Quoll", "Order": "Dasyuromorphia"}]`)3 err := json.Unmarshal(jsonBlob, &animals)4 if err != nil {5 fmt.Println("error:", err)6 }7 fmt.Printf("%+v8}9func main() {10 var jsonBlob = []byte(`[{"Name": "Platypus", "Order": "Monotremata"},{"Name": "Quoll", "Order": "Dasyuromorphia"}]`)11 err := json.Unmarshal(jsonBlob, &animals)12 if err != nil {13 fmt.Println("error:", err)14 }15 fmt.Printf("%+v16}17func main() {18 var jsonBlob = []byte(`[{"Name": "Platypus", "Order": "Monotremata"},{"Name": "Quoll", "Order": "Dasyuromorphia"}]`)19 err := json.Unmarshal(jsonBlob, &animals)20 if err != nil {21 fmt.Println("error:", err)22 }23 fmt.Printf("%+v24}
UnmarshalJSON
Using AI Code Generation
1import (2type Person struct {3}4func main() {5 jsonString := `{"first_name":"John", "last_name":"Doe", "age":24}`6 err := json.Unmarshal([]byte(jsonString), &p)7 if err != nil {8 fmt.Println(err)9 }10 fmt.Println(p.FirstName)11 fmt.Println(p.LastName)12 fmt.Println(p.Age)13}14import (15type Person struct {16}17func main() {18 {"first_name":"John", "last_name":"Doe", "age":24},19 {"first_name":"Bob", "last_name":"Smith", "age":30}20 err := json.Unmarshal([]byte(jsonString), &p)21 if err != nil {22 fmt.Println(err)23 }24 fmt.Println(p[0].FirstName)25 fmt.Println(p[0].LastName)26 fmt.Println(p[0].Age)27 fmt.Println(p[1].FirstName)28 fmt.Println(p[1].LastName)29 fmt.Println(p[1].Age)30}31import (32type Person struct {
UnmarshalJSON
Using AI Code Generation
1import (2type Student struct {3}4func (s *Student) UnmarshalJSON(data []byte) error {5 fmt.Println("UnmarshalJSON called")6 var v []interface{}7 err := json.Unmarshal(data, &v)8 if err != nil {9 }10 s.Name = v[0].(string)11 s.Age = int(v[1].(float64))12}13func main() {14 json.Unmarshal([]byte(`["John", 23]`), &s)15 fmt.Println(s)16}17{John 23}18func (s Student) MarshalJSON() ([]byte, error) {19 fmt.Println("MarshalJSON called")20 return json.Marshal([]interface{}{s.Name, s.Age})21}22import (23type Student struct {24}25func (s Student) MarshalJSON() ([]byte, error) {26 fmt.Println("MarshalJSON called")27 return json.Marshal([]interface{}{s.Name, s.Age})28}29func main() {30 s := Student{"John", 23}31 json.Marshal(s)32}
UnmarshalJSON
Using AI Code Generation
1func main() {2 err := json.Unmarshal([]byte(`{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}`), &x)3 if err != nil {4 fmt.Println(err)5 }6 fmt.Println(x)7}8{Wednesday 6 [Gomez Morticia]}9Related posts: Go: How to Unmarshal JSON into a map[string]interface{}? Go: How to Unmarshal JSON into a struct? Go: How to Unmarshal JSON into a slice? Go: How to Unmarshal JSON into a struct with embedded struct? Go: How to Unmarshal JSON into a struct with embedded pointer to struct? Go: How to Unmarshal JSON into a struct with embedded interface? Go: How to Unmarshal JSON into a struct with embedded pointer to interface? Go: How to Unmarshal JSON into a struct with embedded struct with pointer to interface? Go: How to Unmarshal JSON into a struct with embedded struct with pointer to struct? Go: How to Unmarshal JSON into a struct with pointer to struct? Go: How to Unmarshal JSON into a struct with pointer to interface? Go: How to Unmarshal JSON into a struct with pointer to pointer to struct? Go: How to Unmarshal JSON into a struct with pointer to pointer to interface? Go: How to Unmarshal JSON into a struct with pointer to pointer to pointer to interface? Go: How to Unmarshal JSON into a struct with pointer to pointer to pointer to pointer to interface? Go: How to Unmarshal JSON into a struct with pointer to pointer to pointer to pointer to pointer to interface? Go: How to Unmarshal JSON into a struct with pointer to pointer to pointer to pointer to pointer to pointer to interface? Go: How to Unmarshal JSON into a struct with pointer to pointer to pointer to pointer to pointer to pointer to pointer to interface? Go: How to Unmarshal JSON into a struct with pointer to pointer to pointer to pointer to pointer to pointer to pointer to pointer to interface? Go: How to Unmarshal JSON into a struct with pointer to pointer to pointer to pointer to pointer to pointer to pointer to pointer to pointer to interface? Go: How to Unmarshal JSON into a struct with pointer to pointer to pointer to pointer to pointer to pointer to pointer to pointer to pointer to pointer to interface? Go: How to Unmarshal JSON into a struct with pointer to pointer to pointer to pointer to
UnmarshalJSON
Using AI Code Generation
1func main() {2 var jsonBlob = []byte(`{"name":"types"}`)3 err := json.Unmarshal(jsonBlob, &types)4 if err != nil {5 fmt.Println("error:", err)6 }7 fmt.Printf("%+v", types)8}9{types}10func main() {11 jsonBlob, err := json.Marshal(types)12 if err != nil {13 fmt.Println("error:", err)14 }15 fmt.Printf("%s16}17{"name":"types"}18func main() {19 jsonBlob, err := json.Marshal(types)20 if err != nil {21 fmt.Println("error:", err)22 }23 fmt.Printf("%s24}25{"name":"types"}26func main() {27 jsonBlob, err := json.Marshal(types)28 if err != nil {29 fmt.Println("error:", err)30 }31 fmt.Printf("%s32}33{"name":"types"}34func main() {35 jsonBlob, err := json.Marshal(types)36 if err != nil {37 fmt.Println("error:", err)38 }39 fmt.Printf("%s40}41{"name":"types"}42func main() {43 jsonBlob, err := json.Marshal(types)44 if err != nil {45 fmt.Println("error:", err)46 }47 fmt.Printf("%s48}49{"name":"types"}50func main() {51 jsonBlob, err := json.Marshal(types)
UnmarshalJSON
Using AI Code Generation
1import (2type types struct {3 Types []struct {4 Type struct {5 } `json:"type"`6 } `json:"types"`7}8func main() {9 jsonData := []byte(`10 {11 {12 "type": {13 }14 },15 {16 "type": {17 }18 }19 }20 err := json.Unmarshal(jsonData, &t)21 if err != nil {22 fmt.Println(err)23 }24 fmt.Println(t.Types[0].Type.Name)25 fmt.Println(t.Types[1].Type.Name)26}
UnmarshalJSON
Using AI Code Generation
1import (2func main() {3 jsonString := `{"name":"john", "age": 30}`4 err := json.Unmarshal([]byte(jsonString), &types)5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(types)9}10{john 30}
UnmarshalJSON
Using AI Code Generation
1func main() {2 var data = []byte(`{"name":"John","age":30,"married":true,"hobbies":["music","sports"]}`)3 err := json.Unmarshal(data, &t)4 if err != nil {5 panic(err)6 }7 fmt.Println(t)8}9{John 30 true [music sports]}
UnmarshalJSON
Using AI Code Generation
1func main(){2 json.Unmarshal([]byte(`{"name":"types","age":23,"address":"xyz","salary":50000}`), &types)3 fmt.Println(types)4}5{types 23 xyz 50000}6func main(){7 json, _ := json.Marshal(types)8 fmt.Println(string(json))9}10{"name":"types","age":23,"address":"xyz","salary":50000}
UnmarshalJSON
Using AI Code Generation
1import (2type Person struct {3}4func main() {5 person := Person{"John", 30}6 fmt.Println("Person:", person)7 jsonString, err := json.Marshal(person)8 if err != nil {9 fmt.Println(err)10 }11 fmt.Println("Json String:", string(jsonString))12}13Person: {John 30}14Json String: {"name":"John","age":30}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!