Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NewDecoder error "encoding/json" is right #178

Closed
Jiachang-Cai opened this issue Sep 21, 2017 · 1 comment
Closed

NewDecoder error "encoding/json" is right #178

Jiachang-Cai opened this issue Sep 21, 2017 · 1 comment

Comments

@Jiachang-Cai
Copy link

`package main

import (
"encoding/json"
"github.com/json-iterator/go"
"strings"
"fmt"
)

const jsonStream = {"name":"xxxxx","bundle_id":"com.zonst.majiang","app_platform":"ios","app_category":"100103", "budget_day":1000,"bidding_min":1,"bidding_max":2,"bidding_type":"CPM", "freq":{"open":true,"type":"day","num":100},"speed":1, "targeting":{"vendor":{"open":true,"list":["zonst"]}, "geo_code":{"open":true,"list":["156110100"]},"app_category":{"open":true,"list":["100101"]}, "day_parting":{"open":true,"list":["100409","100410"]},"device_type":{"open":true,"list":["ipad"]}, "os_version":{"open":true,"list":[10]},"carrier":{"open":true,"list":["mobile"]}, "network":{"open":true,"list":["4G"]}},"url":{"tracking_imp_url":"http://www.baidu.com", "tracking_clk_url":"http://www.baidu.com","jump_url":"http://www.baidu.com","deep_link_url":"http://www.baidu.com"}}

func IteratorReadJsonObject(obj interface{}) error {
decoder := jsoniter.NewDecoder(strings.NewReader(jsonStream))
err := decoder.Decode(obj)
if err != nil {
return err
}
return nil
}
func ReadJsonObject(obj interface{}) error {
decoder := json.NewDecoder(strings.NewReader(jsonStream))
err := decoder.Decode(obj)
if err != nil {
return err
}
return nil
}

type IteratorObject struct {
Name *string json:"name"
BundleId *string json:"bundle_id"
AppCategory *string json:"app_category"
AppPlatform *string json:"app_platform"
BudgetDay *float32 json:"budget_day"
BiddingMax *float32 json:"bidding_max"
BiddingMin *float32 json:"bidding_min"
BiddingType *string json:"bidding_type"
Freq *jsoniter.RawMessage json:"freq"
Targeting *jsoniter.RawMessage json:"targeting"
Url *jsoniter.RawMessage json:"url"
Speed *int json:"speed" db:"speed"
}
type Object struct {
Name *string json:"name"
BundleId *string json:"bundle_id"
AppCategory *string json:"app_category"
AppPlatform *string json:"app_platform"
BudgetDay *float32 json:"budget_day"
BiddingMax *float32 json:"bidding_max"
BiddingMin *float32 json:"bidding_min"
BiddingType *string json:"bidding_type"
Freq *json.RawMessage json:"freq"
Targeting *json.RawMessage json:"targeting"
Url *json.RawMessage json:"url"
Speed *int json:"speed" db:"speed"
}

func main() {
iterator_obj, obj := &IteratorObject{}, &Object{}
IteratorReadJsonObject(iterator_obj)
ReadJsonObject(obj)
fmt.Println("iterator:" + string(*iterator_obj.Freq)) //ttp://www.baidu.com","jump_url":"htt
fmt.Println("default:" + string(*obj.Freq)) //{"open":true,"type":"day","num":100}

}`

@taowen
Copy link
Contributor

taowen commented Sep 21, 2017

should be fixed now

@taowen taowen closed this as completed Sep 21, 2017
zhenzou pushed a commit to zhenzou/jsoniter that referenced this issue Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants