normalize returns all attributes of fj and its children (if any).
(fj fastJsonNode)
| 919 | |
| 920 | // normalize returns all attributes of fj and its children (if any). |
| 921 | func (enc *encoder) normalize(fj fastJsonNode) ([]fastJsonNode, error) { |
| 922 | cnt := 0 |
| 923 | chead := enc.children(fj) |
| 924 | for chead != nil { |
| 925 | // Here we are counting all non-scalar children of fj. If there are any such |
| 926 | // children, we will flatten them, otherwise we will return all children. |
| 927 | // We should only consider those children(of fj) for flattening which have |
| 928 | // children and are not facetsParent. |
| 929 | if enc.children(chead) != nil && !enc.getFacetsParent(chead) { |
| 930 | cnt++ |
| 931 | } |
| 932 | chead = chead.next |
| 933 | } |
| 934 | |
| 935 | if cnt == 0 { |
| 936 | // Recursion base case |
| 937 | // There are no children, we can just return slice with fj.child. |
| 938 | return []fastJsonNode{enc.children(fj)}, nil |
| 939 | } |
| 940 | |
| 941 | parentSlice := make([]fastJsonNode, 0, 5) |
| 942 | |
| 943 | // First separate children of fj which are scalar. |
| 944 | var shead, curScalar fastJsonNode |
| 945 | chead = enc.children(fj) |
| 946 | for chead != nil { |
| 947 | if enc.children(chead) != nil && !enc.getFacetsParent(chead) { |
| 948 | chead = chead.next |
| 949 | continue |
| 950 | } |
| 951 | |
| 952 | // Here, add all nodes which have either no children or they are facetsParent. |
| 953 | copyNode := enc.copySingleNode(chead) |
| 954 | if curScalar == nil { |
| 955 | shead, curScalar = copyNode, copyNode |
| 956 | } else { |
| 957 | curScalar.next = copyNode |
| 958 | curScalar = copyNode |
| 959 | } |
| 960 | |
| 961 | chead = chead.next |
| 962 | } |
| 963 | |
| 964 | parentSlice = append(parentSlice, shead) |
| 965 | chead = enc.children(fj) |
| 966 | for chead != nil { |
| 967 | childNode := chead |
| 968 | // Here, exclude all nodes which have either no children or they are facetsParent. |
| 969 | if enc.children(childNode) == nil || enc.getFacetsParent(childNode) { |
| 970 | chead = chead.next |
| 971 | continue |
| 972 | } |
| 973 | |
| 974 | childSlice := make([]fastJsonNode, 0, 5) |
| 975 | for chead != nil && enc.getAttr(childNode) == enc.getAttr(chead) { |
| 976 | childSlice = append(childSlice, enc.children(chead)) |
| 977 | chead = chead.next |
| 978 | } |