// SecureJSON serializes the given struct as Secure JSON into the response body.
// Default prepends "while(1)," to response body if the given struct is array values.
// It also sets the Content-Type as "application/json".
558298
func (c *Context) SecureJSON(code int, obj interface{}) {
	c.Render(code, render.SecureJSON{Prefix: c.engine.secureJsonPrefix, Data: obj})
}
1185940
func (r *Response) WriteJson(value interface{}, contentType string) error {
	return writeJSON(r, http.StatusOK, contentType, value)
}
476037
func (f *Fibaro) getJSON(path string, dataStruct interface{}) error {
	var requestURL = f.URL + path

	req, err := http.NewRequest("GET", requestURL, nil)
	if err != nil {
		return err
	}

	req.SetBasicAuth(f.Username, f.Password)
	resp, err := f.client.Do(req)
	if err != nil {
		return err
	}

	if resp.StatusCode != http.StatusOK {
		err = fmt.Errorf("Response from url \"%s\" has status code %d (%s), expected %d (%s)",
			requestURL,
			resp.StatusCode,
			http.StatusText(resp.StatusCode),
			http.StatusOK,
			http.StatusText(http.StatusOK))
		return err
	}

	defer resp.Body.Close()

	dec := json.NewDecoder(resp.Body)
	err = dec.Decode(&dataStruct)
	if err != nil {
		return err
	}

	return nil
}
617596
func WriteJSON(w http.ResponseWriter, v interface{}) error {
	w.Header().Set("Content-Type", "application/json")

	if err := json.NewEncoder(w).Encode(v); err != nil {
		return err
	}

	return nil
}
394963
func SignJSON(jw *jsonw.Wrapper, key GenericKey) (out string, id keybase1.SigID, lid LinkID, err error) {
	var tmp []byte
	if tmp, err = jw.Marshal(); err != nil {
		return
	}
	out, id, err = key.SignToString(tmp)
	lid = ComputeLinkID(tmp)
	return
}
284302
func BytesToJSON(b *[]byte) (*interface{}, error) {
	var container interface{}
	err := json.Unmarshal(*b, &container)

	if err != nil {
		return nil, err
	}

	return &container, nil
}
1087296
func Secure(opts ...*SecureOpts) fibre.MiddlewareFunc {
	return func(h fibre.HandlerFunc) fibre.HandlerFunc {
		return func(c *fibre.Context) error {

			var config *SecureOpts

			switch len(opts) {
			case 0:
				config = defaultSecureOpts
			default:
				config = opts[0]
			}

			if config.RedirectHTTP && (!c.IsTLS() || c.Request().Header().Get(fibre.HeaderXForwardedProto) == "http") {
				h := c.Request().Host
				u := c.Request().RequestURI
				return c.Redirect(301, "https://"+h+u)
			}

			// This is a socket
			if c.IsSocket() {
				return h(c)
			}

			if config.XSSProtection != "" {
				c.Response().Header().Set(fibre.HeaderXXSSProtection, config.XSSProtection)
			}

			if config.FrameOptions != "" {
				c.Response().Header().Set(fibre.HeaderXFrameOptions, config.FrameOptions)
			}

			if config.ContentTypeOptions != "" {
				c.Response().Header().Set(fibre.HeaderXContentTypeOptions, config.ContentTypeOptions)
			}

			if config.ContentSecurityPolicy != "" {
				c.Response().Header().Set(fibre.HeaderContentSecurityPolicy, config.ContentSecurityPolicy)
			}

			if config.PublicKeyPins != "" {
				c.Response().Header().Set(fibre.HeaderPublicKeyPins, config.PublicKeyPins)
			}

			if (c.IsTLS() || c.Request().Header().Get(fibre.HeaderXForwardedProto) == "https") && config.HSTSMaxAge != 0 {
				if config.HSTSIncludeSubdomains {
					c.Response().Header().Set(fibre.HeaderStrictTransportSecurity, fmt.Sprintf("max-age=%d; includeSubdomains", config.HSTSMaxAge))
				} else {
					c.Response().Header().Set(fibre.HeaderStrictTransportSecurity, fmt.Sprintf("max-age=%d", config.HSTSMaxAge))
				}
			}

			return h(c)

		}
	}
}
529737
func (ua *Uint64Array) MarshalJSON() ([]byte, error) {
	if ua == nil || !ua.Valid {
		return []byte("null"), nil
	}
	return json.Marshal(ua.Uint64Array)
}
999772
func WriteJson(buf JsonStringWriter, s []byte) {
	buf.WriteByte('"')
	start := 0
	for i := 0; i < len(s); {
		if b := s[i]; b < utf8.RuneSelf {
			/*
				if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
					i++
					continue
				}
			*/
			if lt[b] == true {
				i++
				continue
			}

			if start < i {
				buf.Write(s[start:i])
			}
			switch b {
			case '\\', '"':
				buf.WriteByte('\\')
				buf.WriteByte(b)
			case '\n':
				buf.WriteByte('\\')
				buf.WriteByte('n')
			case '\r':
				buf.WriteByte('\\')
				buf.WriteByte('r')
			default:
				// This encodes bytes < 0x20 except for \n and \r,
				// as well as < and >. The latter are escaped because they
				// can lead to security holes when user-controlled strings
				// are rendered into JSON and served to some browsers.
				buf.WriteString(`\u00`)
				buf.WriteByte(hex[b>>4])
				buf.WriteByte(hex[b&0xF])
			}
			i++
			start = i
			continue
		}
		c, size := utf8.DecodeRune(s[i:])
		if c == utf8.RuneError && size == 1 {
			if start < i {
				buf.Write(s[start:i])
			}
			buf.WriteString(`\ufffd`)
			i += size
			start = i
			continue
		}
		// U+2028 is LINE SEPARATOR.
		// U+2029 is PARAGRAPH SEPARATOR.
		// They are both technically valid characters in JSON strings,
		// but don't work in JSONP, which has to be evaluated as JavaScript,
		// and can lead to security holes there. It is valid JSON to
		// escape them, so we do so unconditionally.
		// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
		if c == '\u2028' || c == '\u2029' {
			if start < i {
				buf.Write(s[start:i])
			}
			buf.WriteString(`\u202`)
			buf.WriteByte(hex[c&0xF])
			i += size
			start = i
			continue
		}
		i += size
	}
	if start < len(s) {
		buf.Write(s[start:])
	}
	buf.WriteByte('"')
}
1051093
func (u *Uint64) MarshalJSON() ([]byte, error) {
	if u == nil || !u.Valid {
		return []byte("null"), nil
	}

	return json.Marshal(u.Uint64)
}
619434
func (s shareMesssage) JSON() string {
	s.Status = "success"
	shareMessageBytes, e := json.MarshalIndent(s, "", " ")
	fatalIf(probe.NewError(e), "Failed to marshal into JSON.")

	// JSON encoding escapes ampersand into its unicode character
	// which is not usable directly for share and fails with cloud
	// storage. convert them back so that they are usable.
	shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u0026"), []byte("&"), -1)
	shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u003c"), []byte("<"), -1)
	shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u003e"), []byte(">"), -1)

	return string(shareMessageBytes)
}
954124
func (p *Params) BindJSON(dest interface{}) error {
	value := reflect.ValueOf(dest)
	if value.Kind() != reflect.Ptr {
		paramsLogger.Warn("BindJSON: Not a pointer")
		return errors.New("BindJSON not a pointer")
	}
	if err := json.Unmarshal(p.JSON, dest); err != nil {
		paramsLogger.Warn("BindJSON: Unable to unmarshal request:", "error", err)
		return err
	}
	return nil
}
953406
func (o *SecurityGroupRule) UnmarshalJSON(data []byte) error {
	var jsonName string
	if err := json.Unmarshal(data, &jsonName); err == nil {
		o.ID = &jsonName
		return nil
	}

	var r realSecurityGroupRule
	if err := json.Unmarshal(data, &r); err != nil {
		return err
	}
	*o = SecurityGroupRule(r)
	return nil
}
1181445
func PostJson(postUrl string, data interface{}) (ret string, err error, resp *http.Response) {
	var buf io.Reader
	if data != nil {
		switch val := data.(type) {
		case string:
			buf = bytes.NewBufferString(val)
		case []byte:
			buf = bytes.NewReader(val)
		case json.RawMessage:
			buf = bytes.NewReader([]byte(val))
		case io.Reader:
			buf = val
		case url.Values:
			buf = bytes.NewBufferString(val.Encode())
		default:
			by, err := json.Marshal(data)
			if err != nil {
				return "", err, nil
			}
			buf = bytes.NewReader(by)
		}
	}

	resp, err = http.Post(postUrl, "application/json", buf)
	defer func() {
		if resp != nil && resp.Body != nil {
			resp.Body.Close()
		}
	}()
	if err != nil {
		Log(WARN, err.Error())
		return "", err, resp
	}
	bodyBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err, resp
	}

	return string(bodyBytes), nil, resp
}
248336
func JSON(rw http.ResponseWriter, status int, v interface{}) error {
	if pb, ok := v.(proto.Message); ok {
		return Proto(rw, status, pb)
	}

	b, err := json.MarshalIndent(v, "", "  ")
	if err != nil {
		return err
	}
	rw.Header().Add("Content-Type", "application/json")
	rw.WriteHeader(status)
	_, err = rw.Write(b)
	return err
}
144877
func (m *Middleware) Secure(h http.Handler, v VerifyClaimsFunc) http.Handler {
	secureHandler := func(w http.ResponseWriter, r *http.Request) *jwtError {
		var token string

		authHeader := r.Header.Get("Authorization")
		if authHeader == "" {
			token = r.FormValue("token")
			if token == "" {
				return &jwtError{status: http.StatusUnauthorized, err: ErrMissingToken}
			}
		} else {
			tokenParts := strings.Split(authHeader, " ")
			if len(tokenParts) != 2 {
				return &jwtError{status: http.StatusUnauthorized, err: ErrMalformedToken}
			}
			token = tokenParts[1]
		}

		if status, message, err := m.VerifyToken(token, v, r); err != nil {
			return &jwtError{
				status:  status,
				message: message,
				err:     err,
			}
		}

		// If we make it this far, process the downstream handler
		h.ServeHTTP(w, r)
		return nil
	}
	return errorHandler(secureHandler)
}
1174605
func (n *NodeConfig) MarshalJSON() ([]byte, error) {
	confJSON := nodeConfigJSON{
		ID:              n.ID.String(),
		Name:            n.Name,
		Services:        n.Services,
		Port:            n.Port,
		EnableMsgEvents: n.EnableMsgEvents,
	}
	if n.PrivateKey != nil {
		confJSON.PrivateKey = hex.EncodeToString(crypto.FromECDSA(n.PrivateKey))
	}
	return json.Marshal(confJSON)
}