001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.wsclient.unmarshallers; 021 022 import org.json.simple.JSONArray; 023 import org.json.simple.JSONObject; 024 import org.sonar.wsclient.services.TimeMachine; 025 import org.sonar.wsclient.services.TimeMachineCell; 026 import org.sonar.wsclient.services.TimeMachineColumn; 027 028 public class TimeMachineUnmarshaller extends AbstractUnmarshaller<TimeMachine> { 029 030 protected TimeMachine parse(JSONObject json) { 031 JSONArray cols = JsonUtils.getArray(json, "cols"); 032 JSONArray cells = JsonUtils.getArray(json, "cells"); 033 return new TimeMachine(toColumns(cols), toCells(cells)); 034 } 035 036 private TimeMachineColumn[] toColumns(JSONArray cols) { 037 int size = cols.size(); 038 TimeMachineColumn[] result = new TimeMachineColumn[size]; 039 for (int index = 0; index < size; index++) { 040 JSONObject colJson = (JSONObject) cols.get(index); 041 result[index] = new TimeMachineColumn(index, JsonUtils.getString(colJson, "metric"), null, null); 042 } 043 return result; 044 } 045 046 private TimeMachineCell[] toCells(JSONArray cells) { 047 int size = cells.size(); 048 TimeMachineCell[] result = new TimeMachineCell[size]; 049 for (int i = 0; i < size; i++) { 050 JSONObject cellJson = (JSONObject) cells.get(i); 051 JSONArray valuesJson = JsonUtils.getArray(cellJson, "v"); 052 053 Object[] resultValues = new Object[valuesJson.size()]; 054 for (int indexValue = 0; indexValue < valuesJson.size(); indexValue++) { 055 Object value = valuesJson.get(indexValue); 056 resultValues[indexValue] = value; 057 } 058 result[i] = new TimeMachineCell(JsonUtils.getDateTime(cellJson, "d"), resultValues); 059 } 060 return result; 061 } 062 063 }