當前位置: 首頁>>代碼示例>>Golang>>正文


Golang RawMessage.UnmarshalJSON方法代碼示例

本文整理匯總了Golang中encoding/json.RawMessage.UnmarshalJSON方法的典型用法代碼示例。如果您正苦於以下問題:Golang RawMessage.UnmarshalJSON方法的具體用法?Golang RawMessage.UnmarshalJSON怎麽用?Golang RawMessage.UnmarshalJSON使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在encoding/json.RawMessage的用法示例。


在下文中一共展示了RawMessage.UnmarshalJSON方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Update

func (c RecentCacheUpdater) Update(db *DB) *json.RawMessage {
	users, err := db.RecentlyUpdatedUsers(12)
	if err != nil {
		return nil
	}
	items := make([]recentItem, len(users))
	for index, user := range users {
		logs, err := db.ClimbingLogs(user.ID)
		if err != nil {
			continue
		}
		routeIDs := make([]bson.ObjectId, 0)
		for _, log := range logs {
			if !log.Pending {
				routeIDs = append(routeIDs, log.Route)
			}
		}
		items[index] = recentItem{UserID: user.ID, RouteIDs: routeIDs}
	}
	ret, err := json.Marshal(items)
	if err != nil {
		fmt.Println(err)
		return nil
	}
	j := new(json.RawMessage)
	j.UnmarshalJSON(ret)
	return j
}
開發者ID:songgao,項目名稱:HalfDomeChallenge,代碼行數:28,代碼來源:cacheUpdaters.go

示例2: MockSession

// MockSession creates a new mocked API session returning the JSON message stored in message.
func MockSession(message []byte) (*Session, error) {
	rm := json.RawMessage{}
	err := rm.UnmarshalJSON(message)
	if err != nil {
		return nil, err
	}
	return &Session{
		requester: &mockRequester{message: &rm},
	}, nil
}
開發者ID:UniversityRadioYork,項目名稱:myradio-go,代碼行數:11,代碼來源:api.go

示例3: ToSigned

func (ts SignedTimestamp) ToSigned() (*Signed, error) {
	s, err := cjson.Marshal(ts.Signed)
	if err != nil {
		return nil, err
	}
	signed := json.RawMessage{}
	err = signed.UnmarshalJSON(s)
	if err != nil {
		return nil, err
	}
	sigs := make([]Signature, len(ts.Signatures))
	copy(sigs, ts.Signatures)
	return &Signed{
		Signatures: sigs,
		Signed:     signed,
	}, nil
}
開發者ID:progrium,項目名稱:notary,代碼行數:17,代碼來源:timestamp.go

示例4: ToSigned

func (sp SignedSnapshot) ToSigned() (*Signed, error) {
	s, err := cjson.Marshal(sp.Signed)
	if err != nil {
		return nil, err
	}
	signed := json.RawMessage{}
	err = signed.UnmarshalJSON(s)
	if err != nil {
		return nil, err
	}
	sigs := make([]Signature, len(sp.Signatures))
	copy(sigs, sp.Signatures)
	return &Signed{
		Signatures: sigs,
		Signed:     signed,
	}, nil
}
開發者ID:aaronlehmann,項目名稱:notary,代碼行數:17,代碼來源:snapshot.go

示例5: PostData

func (esi *MockIndex) PostData(typeName string, id string, obj interface{}) (*IndexResponse, error) {
	ok, err := esi.IndexExists()
	if err != nil {
		return nil, err
	}
	if !ok {
		return nil, fmt.Errorf("Index does not exist")
	}
	ok, err = esi.TypeExists(typeName)
	if err != nil {
		return nil, err
	}

	var typ *MockIndexType
	if !ok {
		typ = &MockIndexType{}
		typ.items = make(map[string]*json.RawMessage)
		esi.types[typeName] = typ
	} else {
		typ = esi.types[typeName]
	}

	byts, err := json.Marshal(obj)
	if err != nil {
		return nil, err
	}

	var raw json.RawMessage
	err = raw.UnmarshalJSON(byts)
	if err != nil {
		return nil, err
	}
	typ.items[id] = &raw

	r := &IndexResponse{Created: true, ID: id, Index: esi.name, Type: typeName}
	return r, nil
}
開發者ID:venicegeo,項目名稱:pz-logger,代碼行數:37,代碼來源:mock_index.go

示例6: processOverview


//.........這裏部分代碼省略.........
	for _, region := range regions.Regions {
		fmt.Println("-", *region.RegionName)
	}

	ec2client = ec2.New(session.New(), &aws.Config{Region: aws.String(config.General.DefaultRegion)})

	instances, err := ec2client.DescribeInstances(nil)
	if err != nil {
		panic(err)
	}

	fmt.Println("Instances:")
	instanceCount := 0
	instanceMap := make(map[string]ec2.Instance)
	for _, reservation := range instances.Reservations {
		for _, instance := range reservation.Instances {
			fmt.Println("-", *instance.InstanceId)
			instanceMap[*instance.InstanceId] = *instance
			if *instance.State.Name == "running" {
				instanceCount++
			}
		}
	}
	attachment.Fields = append(attachment.Fields, Field{Title: "Running instances", Value: fmt.Sprintf("%d", instanceCount), Short: true})

	volumes, err := ec2client.DescribeVolumes(nil)
	if err != nil {
		panic(err)
	}
	attachment.Fields = append(attachment.Fields, Field{Title: "Volumes", Value: fmt.Sprintf("%d", len(volumes.Volumes)), Short: true})

	securityGroups, err := ec2client.DescribeSecurityGroups(nil)
	if err != nil {
		panic(err)
	}

	securityGroupMap := make(map[string]ec2.SecurityGroup)
	for _, securityGroup := range securityGroups.SecurityGroups {
		securityGroupMap[*securityGroup.GroupId] = *securityGroup
	}

	vpcs, err := ec2client.DescribeVpcs(nil)
	if err != nil {
		panic(err)
	}

	subnets, err := ec2client.DescribeSubnets(nil)
	if err != nil {
		panic(err)
	}

	routeTables, err := ec2client.DescribeRouteTables(nil)
	if err != nil {
		panic(err)
	}

	vpcPeeringConnections, err := ec2client.DescribeVpcPeeringConnections(nil)
	if err != nil {
		panic(err)
	}

	s3client := s3.New(session.New(), &aws.Config{Region: aws.String(config.General.DefaultRegion)})

	buckets, err := s3client.ListBuckets(nil)
	if err != nil {
		panic(err)
	}
	attachment.Fields = append(attachment.Fields, Field{Title: "S3 buckets", Value: fmt.Sprintf("%d", len(buckets.Buckets)), Short: true})

	rdsclient := rds.New(session.New(), &aws.Config{Region: aws.String(config.General.DefaultRegion)})

	dbInstances, err := rdsclient.DescribeDBInstances(nil)
	if err != nil {
		panic(err)
	}
	attachment.Fields = append(attachment.Fields, Field{Title: "DB instances", Value: fmt.Sprintf("%d", len(dbInstances.DBInstances)), Short: true})

	overview := Overview{Instances: instances, SecurityGroups: securityGroups, Vpcs: vpcs, Subnets: subnets, RouteTables: routeTables, VpcPeeringConnections: vpcPeeringConnections, Buckets: buckets, DBInstances: dbInstances}
	jsonData, err := json.MarshalIndent(overview, "", "  ")
	if err != nil {
		panic(err)
	}

	const outputDir = "/tmp"
	err = os.MkdirAll(outputDir, os.ModeDir)
	if err != nil {
		panic(err)
	}

	err = ioutil.WriteFile(outputDir+"/aws.json", jsonData, 0777)
	if err != nil {
		panic(err)
	}

	var m json.RawMessage
	err = m.UnmarshalJSON(jsonData)
	if err != nil {
		panic(err)
	}
}
開發者ID:ngyewch,項目名稱:aws-dashboard,代碼行數:101,代碼來源:overview.go


注:本文中的encoding/json.RawMessage.UnmarshalJSON方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。