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


Golang jsonhelper.MarshalWithOptions函數代碼示例

本文整理匯總了Golang中github.com/pomack/jsonhelper/go/jsonhelper.MarshalWithOptions函數的典型用法代碼示例。如果您正苦於以下問題:Golang MarshalWithOptions函數的具體用法?Golang MarshalWithOptions怎麽用?Golang MarshalWithOptions使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


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

示例1: Encode

func (p *InMemoryDataStore) Encode(w io.Writer) os.Error {
	v, err := jsonhelper.MarshalWithOptions(p, dm.UTC_DATETIME_FORMAT)
	if err != nil {
		return err
	}
	return json.NewEncoder(w).Encode(v)
}
開發者ID:pombredanne,項目名稱:dsocial.go,代碼行數:7,代碼來源:datastore.go

示例2: ContentTypesProvided

func (p *GeneratePrivateKeyRequestHandler) ContentTypesProvided(req wm.Request, cxt wm.Context) ([]wm.MediaTypeHandler, wm.Request, wm.Context, int, error) {
	gpkc := cxt.(GeneratePrivateKeyContext)
	user := gpkc.User()
	consumer := gpkc.Consumer()
	var userId, consumerId string
	if user != nil {
		userId = user.Id
	}
	if consumer != nil {
		consumerId = consumer.Id
	}
	accessKey, err := p.authDS.StoreAccessKey(dm.NewAccessKey(userId, consumerId))
	gpkc.SetAccessKey(accessKey)
	obj := make(map[string]interface{})
	if user != nil {
		obj["user_id"] = user.Id
		obj["username"] = user.Username
		obj["name"] = user.Name
	}
	if consumer != nil {
		obj["consumer_id"] = consumer.Id
		obj["consumer_short_name"] = consumer.ShortName
	}
	if accessKey != nil {
		obj["access_key_id"] = accessKey.Id
		obj["private_key"] = accessKey.PrivateKey
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	if err != nil {
		return []wm.MediaTypeHandler{apiutil.NewJSONMediaTypeHandler(jsonObj, time.Time{}, "")}, req, gpkc, http.StatusInternalServerError, err
	}
	return []wm.MediaTypeHandler{apiutil.NewJSONMediaTypeHandler(jsonObj, time.Time{}, "")}, req, gpkc, 0, nil
}
開發者ID:pomack,項目名稱:dsocial.go,代碼行數:34,代碼來源:generate_private_key.go

示例3: createJSONHttpResponseFromObj

func createJSONHttpResponseFromObj(req *http.Request, obj interface{}) (resp *http.Response, err error) {
	var b []byte
	if obj != nil {
		obj1, _ := jsonhelper.MarshalWithOptions(obj, GOOGLE_DATETIME_FORMAT)
		jsonobj := obj1.(jsonhelper.JSONObject)
		b, err = json.Marshal(jsonobj)
	}
	if b == nil {
		b = make([]byte, 0)
	}
	buf := bytes.NewBuffer(b)
	headers := make(http.Header)
	headers.Add("Content-Type", "application/json; charset=utf-8")
	resp = new(http.Response)
	resp.Status = "200 OK"
	resp.StatusCode = http.StatusOK
	resp.Proto = "HTTP/1.1"
	resp.ProtoMajor = 1
	resp.ProtoMinor = 1
	resp.Header = headers
	resp.Body = ioutil.NopCloser(buf)
	resp.ContentLength = int64(len(b))
	resp.TransferEncoding = []string{"identity"}
	resp.Close = true
	resp.Request = req
	return
}
開發者ID:pomack,項目名稱:contacts.go,代碼行數:27,代碼來源:mock.go

示例4: HandleJSONObjectInputHandler

func (p *UpdateAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	uac := cxt.(UpdateAccountContext)
	uac.SetFromJSON(inputObj)
	uac.CleanInput(uac.RequestingUser(), uac.OriginalValue())
	//log.Print("[UARH]: HandleJSONObjectInputHandler()")
	errors := make(map[string][]error)
	var obj interface{}
	var err error
	ds := p.ds
	switch uac.Type() {
	case "user":
		if user := uac.User(); user != nil {
			//log.Printf("[UARH]: user is not nil1: %v\n", user)
			user.Validate(false, errors)
			if len(errors) == 0 {
				user, err = ds.UpdateUserAccount(user)
				//log.Printf("[UARH]: user after errors is %v\n", user)
			}
			obj = user
			uac.SetUser(user)
			//log.Printf("[UARH]: setUser to %v\n", user)
		}
	case "consumer":
		if user := uac.Consumer(); user != nil {
			user.Validate(false, errors)
			if len(errors) == 0 {
				user, err = ds.UpdateConsumerAccount(user)
			}
			obj = user
			uac.SetConsumer(user)
		}
	case "external_user":
		if user := uac.ExternalUser(); user != nil {
			user.Validate(false, errors)
			if len(errors) == 0 {
				user, err = ds.UpdateExternalUserAccount(user)
			}
			obj = user
			uac.SetExternalUser(user)
		}
	default:
		return apiutil.OutputErrorMessage("\"type\" must be \"user\", \"consumer\", or \"external_user\"", nil, 400, nil)
	}
	if len(errors) > 0 {
		return apiutil.OutputErrorMessage("Value errors. See result", errors, http.StatusBadRequest, nil)
	}
	if err != nil {
		return apiutil.OutputErrorMessage(err.Error(), nil, http.StatusInternalServerError, nil)
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	//log.Printf("[UARH]: obj was: \n%v\n", obj)
	//log.Printf("[UARH]: Going to output:\n%s\n", jsonObj)
	return apiutil.OutputJSONObject(jsonObj, uac.LastModified(), uac.ETag(), http.StatusOK, nil)
}
開發者ID:pomack,項目名稱:dsocial.go,代碼行數:55,代碼來源:update.go

示例5: HandleJSONObjectInputHandler

func (p *ViewAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	vac := cxt.(ViewAccountContext)

	obj := vac.ToObject()
	var err error
	if err != nil {
		return apiutil.OutputErrorMessage(err.Error(), nil, http.StatusInternalServerError, nil)
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	return apiutil.OutputJSONObject(jsonObj, vac.LastModified(), vac.ETag(), 0, nil)
}
開發者ID:pomack,項目名稱:dsocial.go,代碼行數:12,代碼來源:view.go

示例6: ContentTypesProvided

func (p *ViewAccountRequestHandler) ContentTypesProvided(req wm.Request, cxt wm.Context) ([]wm.MediaTypeHandler, wm.Request, wm.Context, int, error) {
	vac := cxt.(ViewAccountContext)
	obj := vac.ToObject()
	lastModified := vac.LastModified()
	etag := vac.ETag()
	var jsonObj jsonhelper.JSONObject
	if obj != nil {
		theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
		jsonObj, _ = theobj.(jsonhelper.JSONObject)
	}
	return []wm.MediaTypeHandler{apiutil.NewJSONMediaTypeHandler(jsonObj, lastModified, etag)}, req, vac, 0, nil
}
開發者ID:pomack,項目名稱:dsocial.go,代碼行數:12,代碼來源:view.go

示例7: HandleJSONObjectInputHandler

func (p *DeleteAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	dac := cxt.(DeleteAccountContext)

	obj := dac.ToObject()
	var err error
	if !dac.Deleted() {
		_, req, cxt, _, err = p.DeleteResource(req, cxt)
	}
	if err != nil {
		return apiutil.OutputErrorMessage(err.Error(), nil, http.StatusInternalServerError, nil)
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	return apiutil.OutputJSONObject(jsonObj, time.Time{}, "", 0, nil)
}
開發者ID:pomack,項目名稱:dsocial.go,代碼行數:15,代碼來源:delete.go

示例8: Decode

func (p *InMemoryDataStore) Decode(r io.Reader) os.Error {
	err := json.NewDecoder(r).Decode(p)
	if err != nil {
		return err
	}
	m := make(map[string]interface{})
	m[_INMEMORY_CONTACT_COLLECTION_NAME] = new(dm.Contact)
	m[_INMEMORY_CONNECTION_COLLECTION_NAME] = new(dm.Contact)
	m[_INMEMORY_GROUP_COLLECTION_NAME] = new(dm.Group)
	m[_INMEMORY_CHANGESET_COLLECTION_NAME] = new(dm.ChangeSet)
	m[_INMEMORY_CONTACT_FOR_EXTERNAL_CONTACT_COLLECTION_NAME] = new(dm.Contact)
	m[_INMEMORY_GROUP_FOR_EXTERNAL_GROUP_COLLECTION_NAME] = new(dm.Group)
	// These three have different types based on the service
	//m[_INMEMORY_EXTERNAL_CONTACT_COLLECTION_NAME] = "external_contacts"
	//m[_INMEMORY_EXTERNAL_GROUP_COLLECTION_NAME] = "external_group"
	//m[_INMEMORY_USER_TO_CONTACT_SETTINGS_COLLECTION_NAME] = "user_to_contact_settings"
	// These four are all strings
	//m[_INMEMORY_EXTERNAL_TO_INTERNAL_CONTACT_MAPPING_COLLECTION_NAME] = ""
	//m[_INMEMORY_INTERNAL_TO_EXTERNAL_CONTACT_MAPPING_COLLECTION_NAME] = ""
	//m[_INMEMORY_EXTERNAL_TO_INTERNAL_GROUP_MAPPING_COLLECTION_NAME] = ""
	//m[_INMEMORY_INTERNAL_TO_EXTERNAL_GROUP_MAPPING_COLLECTION_NAME] = ""
	m[_INMEMORY_CHANGESETS_TO_APPLY_COLLECTION_NAME] = new(dm.ChangeSetsToApply)
	m[_INMEMORY_CHANGESETS_NOT_CURRENTLY_APPLYABLE_COLLECTION_NAME] = new(dm.ChangeSetsToApply)

	for k, collection := range p.Collections {
		for k1, v1 := range collection.Data {
			m1, _ := jsonhelper.MarshalWithOptions(v1, dm.UTC_DATETIME_FORMAT)
			b1, _ := json.Marshal(m1)
			if obj, ok := m[k]; ok {
				err = json.Unmarshal(b1, obj)
				if err != nil {
					return err
				}
				collection.Data[k1] = obj
			} else {
				collection.Data[k1] = b1
			}
		}
	}
	return nil
}
開發者ID:pombredanne,項目名稱:dsocial.go,代碼行數:41,代碼來源:datastore.go

示例9: HandleJSONObjectInputHandler

func (p *CreateAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, writer io.Writer, inputObj jsonhelper.JSONObject) (int, http.Header, os.Error) {
	cac := cxt.(CreateAccountContext)
	cac.SetFromJSON(inputObj)
	cac.CleanInput(cac.RequestingUser())

	errors := make(map[string][]os.Error)
	var obj interface{}
	var err os.Error
	ds := p.ds
	if user := cac.User(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateUserAccount(user)
		}
		obj = user
	} else if user := cac.Consumer(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateConsumerAccount(user)
		}
		obj = user
	} else if user := cac.ExternalUser(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateExternalUserAccount(user)
		}
		obj = user
	} else {
		return apiutil.OutputErrorMessage(writer, "\"type\" must be \"user\", \"consumer\", or \"external_user\"", nil, 400, nil)
	}
	if len(errors) > 0 {
		return apiutil.OutputErrorMessage(writer, "Value errors. See result", errors, http.StatusBadRequest, nil)
	}
	if err != nil {
		return apiutil.OutputErrorMessage(writer, err.String(), nil, http.StatusInternalServerError, nil)
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	return apiutil.OutputJSONObject(writer, jsonObj, cac.LastModified(), cac.ETag(), 0, nil)
}
開發者ID:pombredanne,項目名稱:dsocial.go,代碼行數:40,代碼來源:create.go

示例10: HandleJSONObjectInputHandler

func (p *CreateAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	cac := cxt.(CreateAccountContext)
	cac.SetFromJSON(inputObj)
	cac.CleanInput(cac.RequestingUser())

	errors := make(map[string][]error)
	var obj map[string]interface{}
	var accessKey *dm.AccessKey
	var err error
	ds := p.ds
	authDS := p.authDS
	if user := cac.User(); user != nil {
		var userPassword *dm.UserPassword
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateUserAccount(user)
			if err == nil && user != nil {
				accessKey, err = authDS.StoreAccessKey(dm.NewAccessKey(user.Id, ""))
			}
		}
		if cac.Password() != "" && user != nil && user.Id != "" {
			userPassword = dm.NewUserPassword(user.Id, cac.Password())
			userPassword.Validate(true, errors)
			if len(errors) == 0 && err == nil {
				userPassword, err = authDS.StoreUserPassword(userPassword)
			}
		}
		obj = make(map[string]interface{})
		obj["user"] = user
		obj["type"] = "user"
		obj["key"] = accessKey
	} else if user := cac.Consumer(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateConsumerAccount(user)
			if err == nil && user != nil {
				accessKey, err = authDS.StoreAccessKey(dm.NewAccessKey("", user.Id))
			}
		}
		obj = make(map[string]interface{})
		obj["consumer"] = user
		obj["type"] = "consumer"
		obj["key"] = accessKey
	} else if user := cac.ExternalUser(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateExternalUserAccount(user)
			if err == nil && user != nil {
				accessKey, err = authDS.StoreAccessKey(dm.NewAccessKey(user.Id, user.ConsumerId))
			}
		}
		obj = make(map[string]interface{})
		obj["external_user"] = user
		obj["type"] = "external_user"
		obj["key"] = accessKey
	} else {
		return apiutil.OutputErrorMessage("\"type\" must be \"user\", \"consumer\", or \"external_user\"", nil, 400, nil)
	}
	if len(errors) > 0 {
		return apiutil.OutputErrorMessage("Value errors. See result", errors, http.StatusBadRequest, nil)
	}
	if err != nil {
		return apiutil.OutputErrorMessage(err.Error(), nil, http.StatusInternalServerError, nil)
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	return apiutil.OutputJSONObject(jsonObj, cac.LastModified(), cac.ETag(), 0, nil)
}
開發者ID:pomack,項目名稱:dsocial.go,代碼行數:68,代碼來源:create.go


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