Пример #1
0
func getConfig(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	json := gabs.New()
	json.Set(readonly == "true", "readonly")

	w.Write([]byte(json.String()))
}
Пример #2
0
func TestShorthand(t *testing.T) {
	json, _ := ParseJSON([]byte(`{
		"outter":{
			"inner":{
				"value":5,
				"value2":10,
				"value3":11
			},
			"inner2":{
			}
		},
		"outter2":{
			"inner":0
		}
	}`))

	missingValue := json.S("outter").S("doesntexist").S("alsodoesntexist").S("inner").S("value").Data()
	if missingValue != nil {
		t.Errorf("missing value was actually found: %v\n", missingValue)
	}

	realValue := json.S("outter").S("inner").S("value2").Data().(float64)
	if realValue != 10 {
		t.Errorf("real value was incorrect: %v\n", realValue)
	}

	_, err := json.S("outter2").Set(json.S("outter").S("inner").Data(), "inner")
	if err != nil {
		t.Errorf("error setting outter2: %v\n", err)
	}

	compare := `{"outter":{"inner":{"value":5,"value2":10,"value3":11},"inner2":{}}` +
		`,"outter2":{"inner":{"value":5,"value2":10,"value3":11}}}`
	out := json.String()
	if out != compare {
		t.Errorf("wrong serialized structure: %v\n", out)
	}

	compare2 := `{"outter":{"inner":{"value":6,"value2":10,"value3":11},"inner2":{}}` +
		`,"outter2":{"inner":{"value":6,"value2":10,"value3":11}}}`

	json.S("outter").S("inner").Set(6, "value")
	out = json.String()
	if out != compare2 {
		t.Errorf("wrong serialized structure: %v\n", out)
	}
}
Пример #3
0
func (s *Server) ListTablesCallbackIterator(cb func(string)) error {
	var lastEvaluatedTableName string

	for {
		query := NewEmptyQuery()
		query.AddExclusiveStartTableName(lastEvaluatedTableName)

		jsonResponse, err := s.queryServer(target("ListTables"), query)
		if err != nil {
			return err
		}

		json, err := simplejson.NewJson(jsonResponse)
		if err != nil {
			return err
		}

		lastEvaluatedTableName = ""
		if json, ok := json.CheckGet("LastEvaluatedTableName"); ok {
			lastEvaluatedTableName, err = json.String()
			if err != nil {
				message := fmt.Sprintf("Unexpected response %s", jsonResponse)
				return errors.New(message)
			}
		}

		response, err := json.Get("TableNames").Array()
		if err != nil {
			message := fmt.Sprintf("Unexpected response %s", jsonResponse)
			return errors.New(message)
		}

		for _, value := range response {
			if t, ok := (value).(string); ok {
				cb(t)
			}
		}
		if lastEvaluatedTableName == "" {
			break
		}
	}

	return nil

}
Пример #4
0
func TestJson(t *testing.T) {
	var sj StructJson
	sj.Name = "slowfei"
	sj.Sex = 25
	sj.Money = 88888.888
	sj.Type.TypeName = "sl_type"
	sj.Type.TypeUUID = "sl_typeuuid"
	sj.Married = false
	sj.ArrayMap = make([]map[string]string, 2, 2)
	sj.ArrayMap[0] = map[string]string{"AKey_1": "AValue_1"}
	sj.ArrayMap[1] = map[string]string{"AKey_2": "AValue_2"}
	sj.Array = []string{"arr_1", "arr_2", "arr_3"}

	json, _ := NewJson(sj, "null", "")
	if json.String() != `{"Name":"slowfei","Sex":25,"Money":88888.888,"Type":{"TypeName":"sl_type","TypeUUID":"sl_typeuuid"},"Married":false,"ArrayMap":[{"AKey_1":"AValue_1"},{"AKey_2":"AValue_2"}],"Array":["arr_1","arr_2","arr_3"],"UID":null}` {
		t.Fail()
	}
	// fmt.Println(json.String())

}
Пример #5
0
func saveDoc(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	json := gabs.New()
	if readonly == "false" {
		r.ParseForm()
		path := r.Form["path"][0]
		markdown := r.Form["doc"][0]
		err := ioutil.WriteFile(path, []byte(markdown), 0644)
		if err != nil {
			json.Set(false, "success")
			json.Set(err.Error(), "error")
		} else {
			json.Set(true, "success")
		}
	} else {
		json.Set(false, "success")
		json.Set("moc is in readonly mode", "error")
	}

	w.Write([]byte(json.String()))
}
Пример #6
0
func TestCreation(t *testing.T) {
	json, _ := ParseJSON([]byte(`{}`))
	inner, err := json.Object("test", "inner")
	if err != nil {
		t.Errorf("Error: %v", err)
		return
	}

	inner.Set(10, "first")
	inner.Set(20, "second")

	inner.Array("array")
	inner.ArrayAppend("first element of the array", "array")
	inner.ArrayAppend(2, "array")
	inner.ArrayAppend("three", "array")

	expected := `{"test":{"inner":{"array":["first element of the array",2,"three"],` +
		`"first":10,"second":20}}}`
	actual := json.String()
	if actual != expected {
		t.Errorf("received incorrect output from json object: %v\n", actual)
	}
}