Esempio n. 1
0
func findFeatureArray(dec *json.Decoder) error {
	for {
		t, err := dec.Token()
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}
		if s, ok := t.(string); ok && s == "features" && dec.More() {
			// we found the features element
			d, err := dec.Token()
			if err != nil {
				return err
			}
			if delim, ok := d.(json.Delim); ok {
				if delim.String() == "[" {
					// we have our start of the array
					break
				} else {
					// A different kind of delimiter
					return fmt.Errorf("Expected features to be an array.")
				}
			}
		}
	}

	if !dec.More() {
		return fmt.Errorf("Cannot find any features.")
	}
	return nil
}
Esempio n. 2
0
func toList(cxDecoder *json.Decoder, w io.Writer) {

	// Node ID to node name map
	nodeMap := make(map[string]int64)

	for {
		_, err := cxDecoder.Token()

		if err == io.EOF {
			break
		} else if err != nil {
			fmt.Println("Error: ", err)
			return
		}

		for cxDecoder.More() {
			token, err := cxDecoder.Token()
			if err != nil {
				fmt.Println(err)
			}

			if token == "nodes" {
				processNode(cxDecoder, nodeMap)
			}
		}
	}

	err := json.NewEncoder(w).Encode(nodeMap)
	if err != nil {
		panic("parse err")
	}
}
Esempio n. 3
0
func decodeArray(dec *json.Decoder) (Array, error) {
	arr := make(Array, 0)
	for {
		tok, err := dec.Token()
		if err != nil {
			return arr, err
		}

		switch token := tok.(type) {
		case json.Delim:
			switch rune(token) {
			case ']':
				return arr, nil
			case '{':
				obj, err := decodeObject(dec)
				if err != nil {
					return arr, err
				}
				arr = append(arr, obj)
			case '[':
				return decodeArray(dec)
			default:
				return nil, Errorf("Unexpected token %s", token)
			}
		}
	}
}
Esempio n. 4
0
func decodeObject(dec *json.Decoder) (Object, error) {
	obj := make(Object)
	for {
		key, err := dec.Token()
		if err != nil {
			return nil, err
		}

		if delim, is := key.(json.Delim); is {
			switch delim {
			case '}':
				return obj, nil
			default:
				return obj, Errorf("Unexpected delim: %s", delim)
			}
		}

		// key is a Value

		value, err := decode(dec)
		obj[key] = value

		if err != nil {
			return obj, err
		}
	}
}
Esempio n. 5
0
func parseSif(cxDecoder *json.Decoder, w *csv.Writer) {

	// Edge slice used for later mapping
	var edges []cx.Edge

	// Node ID to node name map
	nodeMap := make(map[int64]string)

	for {
		_, err := cxDecoder.Token()

		if err == io.EOF {
			break
		} else if err != nil {
			fmt.Println("Error: ", err)
			return
		}

		for cxDecoder.More() {
			token, err := cxDecoder.Token()
			if err != nil {
				fmt.Println(err)
			}

			if token == "nodes" {
				processNode(cxDecoder, nodeMap)
			} else if token == "edges" {
				processEdge(cxDecoder, &edges)
			}
		}
	}

	writeSif(nodeMap, edges, *w)
}
Esempio n. 6
0
func getVal(dec *json.Decoder) (any, error) {
	t, err := dec.Token()
	if err != nil {
		return nil, err
	}

	switch tok := t.(type) {
	case json.Delim:
		switch tok {
		case '[':
			return decToSlice(dec)
		case '{':
			om := New()
			err := om.(*omap).unmarshalJSON(dec)
			if err != nil {
				return nil, err
			}
			_, err = dec.Token() // }
			return om, err
		case ']':
			return nil, endOfSlice
		case '}':
			return nil, fmt.Errorf("unexpected '}'")
		default:
			panic("unreachable code")
		}
	default:
		return tok, nil
	}
}
Esempio n. 7
0
func consumeExpectedToken(decoder *json.Decoder, expected json.Token) error {
	if actual, err := decoder.Token(); err != nil {
		return err
	} else if actual != expected {
		return fmt.Errorf("Expected %v, not %v", expected, actual)
	} else {
		return nil
	}
}
Esempio n. 8
0
func processEdge(decoder *json.Decoder, edges *[]cx.Edge) {
	token, err := decoder.Token()
	if err != nil || token != arrayStart {
		return
	}

	var entry cx.Edge
	for decoder.More() {
		err := decoder.Decode(&entry)
		if err != nil {
			return
		}
		*edges = append(*edges, entry)
	}
}
Esempio n. 9
0
func (m *omap) unmarshalJSON(dec *json.Decoder) error {
	for dec.More() {
		t, err := dec.Token()
		if err != nil {
			return err
		}

		key, isKey := t.(string)
		if !isKey {
			return fmt.Errorf("%t %s is not string(expected key)", t, t)
		}

		val, err := getVal(dec)
		if err != nil {
			return err
		}
		m.Set(key, val)
	}
	return nil
}
Esempio n. 10
0
func (store Store) sliceFromJSONDecoder(dec *json.Decoder) ([]interface{}, error) {
	var slice []interface{}
	for {
		t, err := dec.Token()
		if err != nil {
			return slice, err
		}
		if delim, ok := t.(json.Delim); ok && delim.String() == "]" {
			return slice, nil
		} else if ok && delim.String() == "{" {
			item, err := store.treeBranchFromJSONDecoder(dec)
			if err != nil {
				return slice, err
			}
			slice = append(slice, item)
		} else {
			slice = append(slice, t)
		}
	}
}
Esempio n. 11
0
func ProcJsonString(dec *json.Decoder) int {
	count := 0.0
	for {
		t, err := dec.Token()

		if err == io.EOF {
			break
		}

		if v, ok := t.(float64); ok {
			count += v
		}

		if t == nil {
			break
		}
	}

	return int(count)
}
Esempio n. 12
0
func processNode(decoder *json.Decoder, nodes map[string]int64) {
	_, err := decoder.Token()
	if err != nil {
		return
	}

	var entry cx.Node

	for decoder.More() {
		err := decoder.Decode(&entry)
		if err != nil {
			return
		}

		if entry.N == "" {
			nodes[strconv.FormatInt(entry.ID, 10)] = entry.ID
		} else {
			nodes[entry.N] = entry.ID
		}
	}
}
Esempio n. 13
0
func processNode(decoder *json.Decoder, nodes map[int64]string) {
	token, err := decoder.Token()
	if err != nil || token != arrayStart {
		return
	}

	var entry cx.Node
	for decoder.More() {
		err := decoder.Decode(&entry)
		if err != nil {
			return
		}

		if entry.N == "" {
			nodes[entry.ID] = strconv.FormatInt(entry.ID, 10)
		} else {
			nodes[entry.ID] = entry.N
		}
	}

}
Esempio n. 14
0
func ProcJsonObject(dec *json.Decoder) (float64, bool) {
	count := 0.0
	seen_red := false
	for {
		t, err := dec.Token()
		if err == io.EOF || t == nil {
			break
		}

		if v, ok := t.(json.Delim); ok {
			if v.String() == "{" {
				ncount, seen := ProcJsonObject(dec)
				if !seen {
					count += ncount
				}
			} else if v.String() == "}" {
				return count, seen_red
			} else if v.String() == "[" {
				ncount, _ := ProcJsonObject(dec)
				count += ncount
			} else if v.String() == "]" {
				return count, seen_red
			}
		}

		if v, ok := t.(string); ok {
			if v == "red" {
				seen_red = true
			}
		}

		if v, ok := t.(float64); ok {
			count += v
		}

	}

	return count, seen_red
}
Esempio n. 15
0
func decode(dec *json.Decoder) (Model, error) {
	tok, err := dec.Token()

	if err != nil {
		return nil, err
	}

	switch token := tok.(type) {
	case json.Delim:
		switch rune(token) {
		case '{':
			return decodeObject(dec)
		case '[':
			return decodeArray(dec)
		default:
			return nil, Errorf("Unexpected token %s", token)
		}

	default:
		return token, nil
	}
}
Esempio n. 16
0
func (store Store) treeItemFromJSONDecoder(dec *json.Decoder) (sops.TreeItem, error) {
	var item sops.TreeItem
	key, err := dec.Token()
	if err != nil {
		return item, err
	}
	if k, ok := key.(string); ok {
		item.Key = k
	} else if d, ok := key.(json.Delim); ok && d.String() == "}" {
		return item, errEndOfObject
	} else {
		return item, fmt.Errorf("Expected JSON object key, got %s of type %T instead", key, key)
	}
	value, err := dec.Token()
	if err != nil {
		return item, err
	}
	if delim, ok := value.(json.Delim); ok {
		if delim.String() == "[" {
			v, err := store.sliceFromJSONDecoder(dec)
			if err != nil {
				return item, err
			}
			item.Value = v
		}
		if delim.String() == "{" {
			v, err := store.treeBranchFromJSONDecoder(dec)
			if err != nil {
				return item, err
			}
			item.Value = v
		}
	} else {
		item.Value = value
	}
	return item, nil

}
Esempio n. 17
0
// parse one level of the json translation file, this func will also parse sublevels by calling
func readLevel(jspath string, dec *json.Decoder, tr map[string]string) {
	if jspath == "" {
		t, err := dec.Token()
		if err != nil {
			log.Fatal("translate error 1: ", err)
		}
		if fmt.Sprintf("%T:%v", t, t) != "json.Delim:{" {
			log.Fatal("translate error 1.1: bad translaton file format")
		}
		//fmt.Printf("%s> %T:%v\n", jspath, t, t)
	}

	for dec.More() {
		t1, err1 := dec.Token()
		t2, err2 := dec.Token()

		if err1 != nil {
			log.Fatal("translate error 2: ", err1)
		}

		if err2 != nil {
			log.Fatal("translate error 3: ", err2)
		}

		//fmt.Printf("%s: %T:%v => %T:%v\n", jspath, t1, t1, t2, t2)

		var newpath string
		if jspath == "" {
			newpath = fmt.Sprintf("%v", t1)
		} else {
			newpath = fmt.Sprintf("%s.%v", jspath, t1)
		}
		if fmt.Sprintf("%T:%v", t2, t2) == "json.Delim:{" {
			readLevel(newpath, dec, tr)
		} else {
			tr[newpath] = fmt.Sprintf("%v", t2)
			//fmt.Printf("%s => %v\n", newpath, t2)
		}
	}

	t, err := dec.Token()
	if err != nil {
		log.Fatal("translate error 3=4: ", err)
	}
	if fmt.Sprintf("%T:%v", t, t) != "json.Delim:}" {
		log.Fatal("translate error 4.1: bad translaton file format")
	}
	//fmt.Printf("%s< %T:%v\n", jspath, t, t)

}
Esempio n. 18
0
func run(cxDecoder *json.Decoder, w io.Writer) {

	// Initialize handlers
	handlers := initHandlers()

	// Network Object
	networkAttr := make(map[string]interface{})

	// Elements
	var nodes []cyjs.CyJSNode
	var edges []cyjs.CyJSEdge
	layout := make(map[string]interface{})
	vps := make(map[string]interface{})

	elements := cyjs.Elements{Nodes: nodes, Edges: edges}

	// Temp storage for attributes
	nodeAttrs := make(map[string]interface{})
	edgeAttrs := make(map[string]interface{})

	cxData := make(map[string]interface{})
	// Basic Cytoscape.js object
	cyjsNetwork := cyjs.CyJS{Data: networkAttr, Elements: elements,
		CxData: cxData}

	for {
		_, err := cxDecoder.Token()

		if err == io.EOF {
			break
		} else if err != nil {
			log.Println(err)
			return
		}

		// Decode entry one-by-one.
		for cxDecoder.More() {
			var entry map[string]interface{}
			err := cxDecoder.Decode(&entry)
			if err != nil {
				log.Fatal(err)
			}

			parseCxEntry(handlers, entry, &cyjsNetwork, &nodeAttrs,
				&edgeAttrs, &layout, &vps)
		}
	}

	assignNodeAttr(cyjsNetwork.Elements.Nodes, nodeAttrs, layout)
	assignEdgeAttr(cyjsNetwork.Elements.Edges, edgeAttrs)

	// Add style to net (if style data is available...)
	if vps != nil && reflect.ValueOf(vps).IsNil() == false {
		if vps["style"] != nil {
			cyjsNetwork.Style = vps["style"].([]cyjs.SelectorEntry)
		}
	}

	jsonString, err := json.Marshal(cyjsNetwork)

	if err != nil {
		fmt.Println("ERR: ", err)
	} else {
		//		fmt.Println(string(jsonString))
		w.Write(jsonString)
	}
	//debug()
}