Projekt

Obecné

Profil

Stáhnout (2.66 KB) Statistiky
| Větev: | Tag: | Revize:
1
package jdeserialize;
2

    
3
import java.util.Map;
4

    
5
/**
6
 * <p>
7
 * Represents an array instance, including the values the comprise the array.
8
 * </p>
9
 *
10
 * <p>
11
 * Note that in arrays of primitives, the classdesc will be named "[x", where x
12
 * is the field type code representing the primitive type. See
13
 * jdeserialize.resolveJavaType() for an example of analysis/generation of
14
 * human-readable names from these class names.
15
 * </p>
16
 */
17
public class arrayobj extends contentbase {
18
    /**
19
     * Type of the array instance.
20
     */
21
    public classdesc classdesc;
22

    
23
    /**
24
     * Values of the array, in the order they were read from the stream.
25
     */
26
    public arraycoll data;
27

    
28
    public arrayobj(int handle, classdesc cd, arraycoll data) {
29
        super(contenttype.ARRAY);
30
        this.handle = handle;
31
        this.classdesc = cd;
32
        this.data = data;
33
    }
34

    
35
    public @Override String toJson(String indentation, Map<classdesc, Map<field, Object>> fielddata, boolean child) {
36
        StringBuilder sb = new StringBuilder();
37
        boolean bracketInserted = false;
38
        boolean isLast = false;
39

    
40
        for(int i = 0; i < this.data.size(); i++) {
41
            Object el = this.data.get(i);
42
            
43
            isLast = (i == this.data.size()-1) || (this.data.get(i+1) == null);
44

    
45
            if (el != null) {
46
                if (el instanceof content) {
47
                    if(!bracketInserted){
48
                        sb.append("\n").append(indentation).append("[ \n");
49
                        bracketInserted = true;
50
                        indentation += "\t";
51
                    }
52
                    sb.append(((content) el).toJson(indentation, fielddata, false));
53

    
54
                    if (!isLast) {
55
                        sb.append(",\n");
56
                    }
57
                    else{
58
                        indentation = indentation.replaceFirst("\t", "");
59
                        sb.append("\n").append(indentation).append("]");
60
                        bracketInserted = false;
61
                    }
62
                }else{
63
                    if(!bracketInserted){
64
                        sb.append("[ ");
65
                        bracketInserted = true;
66
                    }
67
                    sb.append(el.toString());
68
                    if (!isLast) {
69
                        sb.append(", ");
70
                    }
71
                    if (isLast) {
72
                        sb.append(" ]");
73
                    }
74
                }
75
                
76
            }
77
        }
78
        return sb.toString();
79
    }
80

    
81
    public String toString() {
82
        return "[array " + jdeserialize.hex(handle) + " classdesc " + classdesc.toString() + ": " + data.toString()
83
                + "]";
84
    }
85
}
(6-6/20)