()
| 129 | } |
| 130 | |
| 131 | func (e *exporter) toJSON() (*bpb.KVList, error) { |
| 132 | bp := new(bytes.Buffer) |
| 133 | // We could output more compact JSON at the cost of code complexity. |
| 134 | // Leaving it simple for now. |
| 135 | |
| 136 | writeFacets := func(pfacets []*api.Facet) error { |
| 137 | for _, fct := range pfacets { |
| 138 | fmt.Fprintf(bp, `,"%s|%s":`, e.attr, fct.Key) |
| 139 | |
| 140 | str, err := facetToString(fct) |
| 141 | if err != nil { |
| 142 | glog.Errorf("Ignoring error: %+v", err) |
| 143 | return nil |
| 144 | } |
| 145 | |
| 146 | tid, err := facets.TypeIDFor(fct) |
| 147 | if err != nil { |
| 148 | glog.Errorf("Error getting type id from facet %#v: %v", fct, err) |
| 149 | continue |
| 150 | } |
| 151 | |
| 152 | if !tid.IsNumber() { |
| 153 | str = escapedString(str) |
| 154 | } |
| 155 | |
| 156 | fmt.Fprint(bp, str) |
| 157 | } |
| 158 | return nil |
| 159 | } |
| 160 | |
| 161 | continuing := false |
| 162 | mapStart := fmt.Sprintf(" {\"uid\":"+uidFmtStrJson+`,"namespace":"0x%x"`, e.uid, e.namespace) |
| 163 | err := e.pl.Iterate(e.readTs, 0, func(p *pb.Posting) error { |
| 164 | if continuing { |
| 165 | fmt.Fprint(bp, ",\n") |
| 166 | } else { |
| 167 | continuing = true |
| 168 | } |
| 169 | |
| 170 | fmt.Fprint(bp, mapStart) |
| 171 | if p.PostingType == pb.Posting_REF { |
| 172 | fmt.Fprintf(bp, `,"%s":[`, e.attr) |
| 173 | fmt.Fprintf(bp, "{\"uid\":"+uidFmtStrJson, p.Uid) |
| 174 | if err := writeFacets(p.Facets); err != nil { |
| 175 | return errors.Wrap(err, "While writing facets for posting_REF") |
| 176 | } |
| 177 | fmt.Fprint(bp, "}]") |
| 178 | } else { |
| 179 | if p.PostingType == pb.Posting_VALUE_LANG { |
| 180 | fmt.Fprintf(bp, `,"%s@%s":`, e.attr, string(p.LangTag)) |
| 181 | } else { |
| 182 | fmt.Fprintf(bp, `,"%s":`, e.attr) |
| 183 | } |
| 184 | |
| 185 | val := types.Val{Tid: types.TypeID(p.ValType), Value: p.Value} |
| 186 | str, err := valToStr(val) |
| 187 | if err != nil { |
| 188 | // Copying this behavior from RDF exporter. |
no test coverage detected