Example usage for com.google.gson JsonElement getAsInt

List of usage examples for com.google.gson JsonElement getAsInt

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsInt.

Prototype

public int getAsInt() 

Source Link

Document

convenience method to get this element as a primitive integer value.

Usage

From source file:dao.WorkshopDAO.java

public Workshop retrieveWorkshop(String givenEmail, int staffId, String token)
        throws UnsupportedEncodingException, IOException {
    Workshop ws = null;//  w ww.jav  a  2 s.c  om
    String url = "http://119.81.43.85/erp/workshop/get_shop_by_id_or_email";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));
    urlParameters.add(new BasicNameValuePair("email", givenEmail));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();

    JsonElement shopElement = jobj.get("payload");
    JsonObject shop = null;
    if (shopElement.isJsonNull()) {
        return ws;
    } else {
        shop = shopElement.getAsJsonObject().getAsJsonObject("shop");
        JsonElement attElement = shop.get("id");
        int id = 0;
        if (!attElement.isJsonNull()) {
            id = attElement.getAsInt();
        }
        attElement = shop.get("name");
        String name = "";
        if (!attElement.isJsonNull()) {
            name = attElement.getAsString();
        }
        attElement = shop.get("email");
        String email = "";
        if (!attElement.isJsonNull()) {
            email = attElement.getAsString();
        }
        attElement = shop.get("description");
        String description = "";
        if (!attElement.isJsonNull()) {
            description = attElement.getAsString();
        }
        attElement = shop.get("website");
        String website = "";
        if (!attElement.isJsonNull()) {
            website = attElement.getAsString();
        }
        attElement = shop.get("address");
        String address = "";
        if (!attElement.isJsonNull()) {
            address = attElement.getAsString();
        }
        attElement = shop.get("opening_hour_full");
        String openingHour = "";
        if (!attElement.isJsonNull()) {
            openingHour = attElement.getAsString();
        }
        attElement = shop.get("opening_hour_format");
        String openingHourFormat = "";
        if (!attElement.isJsonNull()) {
            openingHourFormat = attElement.getAsString();
        }
        attElement = shop.get("latitude");
        double latitude = 0.0;
        if (!attElement.isJsonNull()) {
            latitude = attElement.getAsDouble();
        }
        attElement = shop.get("longitude");
        double longitude = 0.0;
        if (!attElement.isJsonNull()) {
            longitude = attElement.getAsDouble();
        }
        attElement = shop.get("contact");
        String contact = "";
        if (!attElement.isJsonNull()) {
            contact = attElement.getAsString();
        }
        attElement = shop.get("contact2");
        String contact2 = "";
        if (!attElement.isJsonNull()) {
            contact2 = attElement.getAsString();
        }
        attElement = shop.get("location");
        String location = "";
        if (!attElement.isJsonNull()) {
            location = attElement.getAsString();
        }
        attElement = shop.get("specialize");
        String specialize = "";
        if (!attElement.isJsonNull()) {
            specialize = attElement.getAsString();
        }
        attElement = shop.get("category");
        String category = "";
        if (!attElement.isJsonNull()) {
            category = attElement.getAsString();
        }
        attElement = shop.get("brand_carried");
        String brandsCarried = "";
        if (!attElement.isJsonNull()) {
            brandsCarried = attElement.getAsString();
        }
        attElement = shop.get("remark");
        String remark = "";
        if (!attElement.isJsonNull()) {
            remark = attElement.getAsString();
        }
        attElement = shop.get("status");
        int status = 0;
        if (!attElement.isJsonNull()) {
            status = attElement.getAsInt();
        }
        ws = new Workshop(id, email, name, description, website, address, openingHour, openingHourFormat,
                latitude, longitude, contact, contact2, location, specialize, category, brandsCarried, remark,
                status);
    }
    return ws;
}

From source file:dao.WorkshopDAO.java

public Workshop retrieveWorkshop(int givenID, int staffId, String token) throws IOException {
    Workshop ws = null;//from   ww  w .j  a  v  a  2s . c o  m
    String url = "http://119.81.43.85/erp/workshop/get_shop_by_id_or_email";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));
    urlParameters.add(new BasicNameValuePair("id", givenID + ""));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();

    JsonElement shopElement = jobj.get("payload");
    JsonObject shop = null;
    if (shopElement.isJsonNull()) {
        return ws;
    } else {
        shop = shopElement.getAsJsonObject().getAsJsonObject("shop");
        JsonElement attElement = shop.get("id");
        int id = 0;
        if (!attElement.isJsonNull()) {
            id = attElement.getAsInt();
        }
        attElement = shop.get("name");
        String name = "";
        if (!attElement.isJsonNull()) {
            name = attElement.getAsString();
        }
        attElement = shop.get("email");
        String email = "";
        if (!attElement.isJsonNull()) {
            email = attElement.getAsString();
        }
        attElement = shop.get("description");
        String description = "";
        if (!attElement.isJsonNull()) {
            description = attElement.getAsString();
        }
        attElement = shop.get("website");
        String website = "";
        if (!attElement.isJsonNull()) {
            website = attElement.getAsString();
        }
        attElement = shop.get("address");
        String address = "";
        if (!attElement.isJsonNull()) {
            address = attElement.getAsString();
        }
        attElement = shop.get("opening_hour_full");
        String openingHour = "";
        if (!attElement.isJsonNull()) {
            openingHour = attElement.getAsString();
        }
        attElement = shop.get("opening_hour_format");
        String openingHourFormat = "";
        if (!attElement.isJsonNull()) {
            openingHourFormat = attElement.getAsString();
        }
        attElement = shop.get("latitude");
        double latitude = 0.0;
        if (!attElement.isJsonNull()) {
            latitude = attElement.getAsDouble();
        }
        attElement = shop.get("longitude");
        double longitude = 0.0;
        if (!attElement.isJsonNull()) {
            longitude = attElement.getAsDouble();
        }
        attElement = shop.get("contact");
        String contact = "";
        if (!attElement.isJsonNull()) {
            contact = attElement.getAsString();
        }
        attElement = shop.get("contact2");
        String contact2 = "";
        if (!attElement.isJsonNull()) {
            contact2 = attElement.getAsString();
        }
        attElement = shop.get("location");
        String location = "";
        if (!attElement.isJsonNull()) {
            location = attElement.getAsString();
        }
        attElement = shop.get("specialize");
        String specialize = "";
        if (!attElement.isJsonNull()) {
            specialize = attElement.getAsString();
        }
        attElement = shop.get("category");
        String category = "";
        if (!attElement.isJsonNull()) {
            category = attElement.getAsString();
        }
        attElement = shop.get("brand_carried");
        String brandsCarried = "";
        if (!attElement.isJsonNull()) {
            brandsCarried = attElement.getAsString();
        }
        attElement = shop.get("remark");
        String remark = "";
        if (!attElement.isJsonNull()) {
            remark = attElement.getAsString();
        }
        attElement = shop.get("status");
        int status = 0;
        if (!attElement.isJsonNull()) {
            status = attElement.getAsInt();
        }
        ws = new Workshop(id, email, name, description, website, address, openingHour, openingHourFormat,
                latitude, longitude, contact, contact2, location, specialize, category, brandsCarried, remark,
                status);
    }
    return ws;
}

From source file:dao.WorkshopDAO.java

public ArrayList<Workshop> retrieveAllWorkshops(int staffId, String token)
        throws UnsupportedEncodingException, IOException {
    ArrayList<Workshop> allWorkshops = new ArrayList<Workshop>();
    String url = "http://119.81.43.85/erp/workshop/get_all_shop";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);//from  w ww.  j  a  v a2 s.c om
    }
    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();
    JsonArray arr = (jobj.getAsJsonObject("payload")).getAsJsonArray("shops");

    for (int i = 0; i < arr.size(); i++) {
        JsonElement workshop = arr.get(i);
        JsonObject shop = workshop.getAsJsonObject();
        JsonElement attElement = shop.get("id");
        int id = 0;
        if (!attElement.isJsonNull()) {
            id = attElement.getAsInt();
        }
        attElement = shop.get("name");
        String name = "";
        if (!attElement.isJsonNull()) {
            name = attElement.getAsString();
        }
        attElement = shop.get("email");
        String email = "";
        if (!attElement.isJsonNull()) {
            email = attElement.getAsString();
        }
        attElement = shop.get("description");
        String description = "";
        if (!attElement.isJsonNull()) {
            description = attElement.getAsString();
        }
        attElement = shop.get("website");
        String website = "";
        if (!attElement.isJsonNull()) {
            website = attElement.getAsString();
        }
        attElement = shop.get("address");
        String address = "";
        if (!attElement.isJsonNull()) {
            address = attElement.getAsString();
        }
        attElement = shop.get("opening_hour_full");
        String openingHour = "";
        if (!attElement.isJsonNull()) {
            openingHour = attElement.getAsString();
        }
        attElement = shop.get("opening_hour_format");
        String openingHourFormat = "";
        if (!attElement.isJsonNull()) {
            openingHourFormat = attElement.getAsString();
        }
        attElement = shop.get("latitude");
        double latitude = 0.0;
        if (!attElement.isJsonNull()) {
            latitude = attElement.getAsDouble();
        }
        attElement = shop.get("longitude");
        double longitude = 0.0;
        if (!attElement.isJsonNull()) {
            longitude = attElement.getAsDouble();
        }
        attElement = shop.get("contact");
        String contact = "";
        if (!attElement.isJsonNull()) {
            contact = attElement.getAsString();
        }
        attElement = shop.get("contact2");
        String contact2 = "";
        if (!attElement.isJsonNull()) {
            contact2 = attElement.getAsString();
        }
        attElement = shop.get("location");
        String location = "";
        if (!attElement.isJsonNull()) {
            location = attElement.getAsString();
        }
        attElement = shop.get("specialize");
        String specialize = "";
        if (!attElement.isJsonNull()) {
            specialize = attElement.getAsString();
        }
        attElement = shop.get("category");
        String category = "";
        if (!attElement.isJsonNull()) {
            category = attElement.getAsString();
        }
        attElement = shop.get("brand_carried");
        String brandsCarried = "";
        if (!attElement.isJsonNull()) {
            brandsCarried = attElement.getAsString();
        }
        attElement = shop.get("remark");
        String remark = "";
        if (!attElement.isJsonNull()) {
            remark = attElement.getAsString();
        }
        attElement = shop.get("status");
        int status = 0;
        if (!attElement.isJsonNull()) {
            status = attElement.getAsInt();
        }
        //int status = attElement.getAsInt();
        Workshop ws = new Workshop(id, email, name, description, website, address, openingHour,
                openingHourFormat, latitude, longitude, contact, contact2, location, specialize, category,
                brandsCarried, remark, status);

        allWorkshops.add(ws);
    }
    return allWorkshops;
}

From source file:de.azapps.mirakel.model.list.ListMirakel.java

License:Open Source License

public static ListMirakel parseJson(JsonObject el) {
    ListMirakel t = null;//from   w  ww.  j  ava2  s . com
    JsonElement id = el.get("id");
    if (id != null)
        // use old List from db if existing
        t = ListMirakel.getList(id.getAsInt());
    if (t == null) {
        t = new ListMirakel();
    }
    JsonElement j = el.get("name");
    if (j != null)
        t.setName(j.getAsString());

    j = el.get("lft");
    if (j != null)
        t.setLft(j.getAsInt());

    j = el.get("rgt");
    if (j != null)
        t.setRgt(j.getAsInt());

    j = el.get("lft");
    if (j != null)
        t.setLft(j.getAsInt());

    j = el.get("updated_at");
    if (j != null) {
        t.setUpdatedAt(j.getAsString().replace(":", ""));
    }

    j = el.get("sort_by");
    if (j != null)
        t.setSortBy(j.getAsInt());

    return t;
}

From source file:de.azapps.mirakel.model.list.meta.SetDeserializer.java

License:Open Source License

@Override
public T deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) {
    if (json.isJsonObject()) {
        List<Integer> content = null;
        Boolean negated = null;// initialize with stuff to mute the
        // compiler
        for (final Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {

            if (entry.getValue().isJsonArray() && "content".equals(entry.getKey())) {
                content = new ArrayList<>(entry.getValue().getAsJsonArray().size());
                for (final JsonElement el : ((JsonArray) entry.getValue())) {
                    if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isNumber()) {
                        content.add(el.getAsInt());
                    }/*  ww w  . j a  v a2s .  c  o  m*/
                }
            } else if (entry.getValue().isJsonPrimitive()
                    && ("isNegated".equals(entry.getKey()) || "isSet".equals(entry.getKey()))) {
                negated = entry.getValue().getAsBoolean();
            } else {
                throw new JsonParseException("unknown format");
            }
        }
        if ((content != null) && (negated != null)) {
            try {
                return (T) clazz.getConstructor(boolean.class, List.class).newInstance(negated, content);
            } catch (IllegalAccessException | InstantiationException | NoSuchMethodException
                    | InvocationTargetException e) {
                throw new JsonParseException("Could not create new SetProperty", e);
            }

        }
    }
    throw new JsonParseException("unknown format");
}

From source file:de.azapps.mirakel.model.task.Task.java

License:Open Source License

/**
 * Parses a JSON-Object to a task// w ww .j  av a2  s. c  o m
 * 
 * @param el
 * @return
 */
public static Task parse_json(JsonObject el, AccountMirakel account) {
    Task t = null;
    JsonElement id = el.get("id");
    if (id != null) {
        t = Task.get(id.getAsLong());
    } else {
        id = el.get("uuid");
        if (id != null)
            t = Task.getByUUID(id.getAsString());
    }
    if (t == null) {
        t = new Task();
    }

    // Name
    Set<Entry<String, JsonElement>> entries = el.entrySet();
    for (Entry<String, JsonElement> entry : entries) {
        String key = entry.getKey();
        JsonElement val = entry.getValue();
        if (key == null || key.equals("id"))
            continue;
        if (key.equals("uuid")) {
            t.setUUID(val.getAsString());
        } else if (key.equals("name") || key.equals("description")) {
            t.setName(val.getAsString());
        } else if (key.equals("content")) {
            String content = val.getAsString();
            if (content == null)
                content = "";
            t.setContent(content);
        } else if (key.equals("priority")) {
            String prioString = val.getAsString().trim();
            if (prioString.equals("L") && t.getPriority() != -1) {
                t.setPriority(-2);
            } else if (prioString.equals("M")) {
                t.setPriority(1);
            } else if (prioString.equals("H")) {
                t.setPriority(2);
            } else if (!prioString.equals("L")) {
                t.setPriority(val.getAsInt());
            }
        } else if (key.equals("list_id")) {
            ListMirakel list = ListMirakel.getList(val.getAsInt());
            if (list == null)
                list = SpecialList.firstSpecial().getDefaultList();
            t.setList(list);
        } else if (key.equals("project")) {
            ListMirakel list = ListMirakel.findByName(val.getAsString());
            if (list == null || list.getAccount().getId() != account.getId()) {
                list = ListMirakel.newList(val.getAsString(), ListMirakel.SORT_BY_OPT, account);
            }
            t.setList(list);
        } else if (key.equals("created_at")) {
            t.setCreatedAt(val.getAsString().replace(":", ""));
        } else if (key.equals("updated_at")) {
            t.setUpdatedAt(val.getAsString().replace(":", ""));
        } else if (key.equals("entry")) {
            t.setCreatedAt(parseDate(val.getAsString(), context.getString(R.string.TWDateFormat)));
        } else if (key.equals("modification")) {
            t.setUpdatedAt(parseDate(val.getAsString(), context.getString(R.string.TWDateFormat)));
        } else if (key.equals("done")) {
            t.setDone(val.getAsBoolean());
        } else if (key.equals("status")) {
            String status = val.getAsString();
            if (status.equals("pending")) {
                t.setDone(false);
            } else if (status.equals("deleted")) {
                t.setSyncState(SYNC_STATE.DELETE);
            } else {
                t.setDone(true);
            }
            t.addAdditionalEntry(key, val.getAsString());
            // TODO don't ignore waiting and recurring!!!
        } else if (key.equals("due")) {
            Calendar due = parseDate(val.getAsString(), "yyyy-MM-dd");
            if (due == null) {
                due = parseDate(val.getAsString(), context.getString(R.string.TWDateFormat));
                if (val.getAsString().contains("T230000Z")) {
                    // Hope this works around a tw-bug
                    // if you add a task with due:today, the task which will be sync will be
                    // due:yesterday:23:00
                    due.add(Calendar.DAY_OF_MONTH, 1);
                }
            }
            t.setDue(due);
        } else if (key.equals("reminder")) {
            Calendar reminder = parseDate(val.getAsString(), "yyyy-MM-dd");
            if (reminder == null) {
                reminder = parseDate(val.getAsString(), context.getString(R.string.TWDateFormat));
            }
            t.setReminder(reminder);
        } else if (key.equals("annotations")) {
            String content = "";
            try {
                JsonArray annotations = val.getAsJsonArray();
                boolean first = true;
                for (JsonElement a : annotations) {
                    if (first)
                        first = false;
                    else
                        content += "\n";
                    content += a.getAsJsonObject().get("description").getAsString();
                }
            } catch (Exception e) {
                Log.e(TAG, "cannot parse json");
            }
            t.setContent(content);
        } else if (key.equals("content")) {
            t.setContent(val.getAsString());
        } else if (key.equals("sync_state")) {
            t.setSyncState(SYNC_STATE.parseInt(val.getAsInt()));
        } else if (key.equals("depends")) {
            t.setDependencies(val.getAsString().split(","));
        } else {
            t.addAdditionalEntry(key, val.getAsString());
        }
    }
    if (t.getList() == null) {
        ListMirakel l = MirakelPreferences.getImportDefaultList(true);
        t.setList(l);
    }
    return t;
}

From source file:de.azapps.mirakel.model.task.TaskDeserializer.java

License:Open Source License

@Override
public Task deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject el = json.getAsJsonObject();
    Optional<ListMirakel> taskList = absent();
    final JsonElement id = el.get("id");
    if (id == null) {
        throw new JsonParseException("Json malformed");
    }//from  w  w  w. jav  a  2s.c o m
    final Task task = Task.get(id.getAsLong()).or(new Task());
    // Name
    final Set<Entry<String, JsonElement>> entries = el.entrySet();
    for (final Entry<String, JsonElement> entry : entries) {
        final String key = entry.getKey();
        final JsonElement val = entry.getValue();
        if ((key == null) || "id".equalsIgnoreCase(key)) {
            continue;
        }
        switch (key.toLowerCase()) {
        case "name":
            task.setName(val.getAsString());
            break;
        case "content":
            String content = val.getAsString();
            if (content == null) {
                content = "";
            }
            task.setContent(content);
            break;
        case "priority":
            task.setPriority((int) val.getAsFloat());
            break;
        case "progress":
            task.setProgress((int) val.getAsDouble());
            break;
        case "list_id":
            taskList = ListMirakel.get(val.getAsInt());
            if (!taskList.isPresent()) {
                taskList = fromNullable(SpecialList.firstSpecialSafe().getDefaultList());
            }
            break;
        case "created_at":
            try {
                task.setCreatedAt(DateTimeHelper.parseDateTime(val.getAsString().replace(":", "")));
            } catch (final ParseException e) {
                Log.wtf(TAG, "invalid dateformat: ", e);
            }
            break;
        case "updated_at":
            try {
                task.setUpdatedAt(DateTimeHelper.parseDateTime(val.getAsString().replace(":", "")));
            } catch (final ParseException e) {
                Log.wtf(TAG, "invalid dateformat: ", e);
            }
            break;
        case "done":
            task.setDone(val.getAsBoolean());
            break;
        case "due":
            try {
                task.setDue(of(DateTimeHelper.createLocalCalendar(val.getAsLong())));
            } catch (final NumberFormatException ignored) {
                task.setDue(Optional.<Calendar>absent());
            }
            break;
        case "reminder":
            try {
                task.setReminder(of(DateTimeHelper.createLocalCalendar(val.getAsLong())));
            } catch (final NumberFormatException ignored) {
                task.setReminder(Optional.<Calendar>absent());
            }
            break;
        case "tags":
            handleTags(task, val);
            break;
        case "sync_state":
            task.setSyncState(DefinitionsHelper.SYNC_STATE.valueOf((short) val.getAsFloat()));
            break;
        case "show_recurring":
            task.setIsRecurringShown(val.getAsBoolean());
            break;
        default:
            handleAdditionalEntries(task, key, val);
            break;
        }
    }
    if (!taskList.isPresent()) {
        taskList = of(ListMirakel.safeFirst());
    }
    task.setList(taskList.get(), true);
    return task;
}

From source file:de.azapps.mirakel.model.task.TaskDeserializer.java

License:Open Source License

private static void handleAdditionalEntries(final Task t, final String key, final JsonElement val) {
    if (val.isJsonPrimitive()) {
        final JsonPrimitive p = (JsonPrimitive) val;
        if (p.isBoolean()) {
            t.addAdditionalEntry(key, String.valueOf(val.getAsBoolean()));
        } else if (p.isNumber()) {
            t.addAdditionalEntry(key, String.valueOf(val.getAsInt()));
        } else if (p.isJsonNull()) {
            t.addAdditionalEntry(key, "null");
        } else if (p.isString()) {
            t.addAdditionalEntry(key, '"' + val.getAsString() + '"');
        } else {//from   w w w  .  j  av a2  s .  co  m
            Log.w(TAG, "unknown json-type");
        }
    } else if (val.isJsonArray()) {
        final JsonArray a = (JsonArray) val;
        StringBuilder s = new StringBuilder("[");
        boolean first = true;
        for (final JsonElement e : a) {
            if (e.isJsonPrimitive()) {
                final JsonPrimitive p = (JsonPrimitive) e;
                final String add;
                if (p.isBoolean()) {
                    add = String.valueOf(p.getAsBoolean());
                } else if (p.isNumber()) {
                    add = String.valueOf(p.getAsInt());
                } else if (p.isString()) {
                    add = '"' + p.getAsString() + '"';
                } else if (p.isJsonNull()) {
                    add = "null";
                } else {
                    Log.w(TAG, "unknown json-type");
                    break;
                }
                s.append(first ? "" : ",").append(add);
                first = false;
            } else {
                Log.w(TAG, "unknown json-type");
            }
        }
        t.addAdditionalEntry(key, s + "]");
    } else {
        Log.w(TAG, "unknown json-type");
    }
}

From source file:de.baydev.hueemulator.network.resources.service.ConfigService.java

License:Open Source License

public Object updateConfig(String data) {
    LOGGER.info("putState called");
    List<HueResponse> responses = new ArrayList<>();

    if (data != null) {

        LOGGER.log(Level.INFO, "data={0}", data);

        HueConfig config = getConfig();//from  w  w  w .  jav a  2  s.c o m

        JsonUtil.toJsonObject(data).entrySet().forEach(k -> {

            JsonElement val = k.getValue();

            switch (k.getKey()) {
            case "name":
                config.setName(val.getAsString());
                responses.addAll(responseSimpleSuccess("/config/name", config.getName()));
                break;
            case "proxyaddress":
                config.setProxyaddress(val.getAsString());
                responses.addAll(responseSimpleSuccess("/config/proxyaddress", config.getProxyaddress()));
                break;
            case "proxyport":
                config.setProxyport(val.getAsInt());
                responses.addAll(responseSimpleSuccess("/config/proxyport", config.getProxyport()));
                break;
            case "swupdate":
                config.setSwupdate(JsonUtil.fromJson(val.getAsJsonObject().toString(), HueSwUpdate.class));
                responses.addAll(responseSimpleSuccess("/config/swupdate", config.getSwupdate()));
                break;
            case "linkbutton":
                config.setLinkbutton(val.getAsBoolean());
                responses.addAll(responseSimpleSuccess("/config/linkbutton", config.getLinkbutton()));
                break;
            case "ipaddress":
                config.setIpaddress(val.getAsString());
                responses.addAll(responseSimpleSuccess("/config/ipaddress", config.getIpaddress()));
                break;
            case "netmask":
                config.setNetmask(val.getAsString());
                responses.addAll(responseSimpleSuccess("/config/netmask", config.getNetmask()));
                break;
            case "gateway":
                config.setGateway(val.getAsString());
                responses.addAll(responseSimpleSuccess("/config/gateway", config.getGateway()));
                break;
            case "dhcp":
                config.setDhcp(val.getAsBoolean());
                responses.addAll(responseSimpleSuccess("/config/dhcp", config.getDhcp()));
                break;
            case "UTC":
                config.setUTC(val.getAsString());
                responses.addAll(responseSimpleSuccess("/config/UTC", config.getUTC()));
                break;
            case "timezone":
                config.setTimezone(val.getAsString());
                responses.addAll(responseSimpleSuccess("/config/timezone", config.getTimezone()));
                break;
            case "touchlink":
                /**
                 * TODO
                 */
                break;
            default:
            }
        });

        HueLoader.getInstance().getBridge().setConfig(config);

    }
    return responses;
}

From source file:de.baydev.hueemulator.network.resources.service.ResourceLinkService.java

License:Open Source License

public List<HueResponse> putAttributes(String id, String data) {
    List<HueResponse> responses = new ArrayList<>();
    HueResourceLink item = manager.getItems().get(id);

    if (item != null) {

        JsonUtil.toJsonObject(data).entrySet().forEach(k -> {
            JsonElement val = k.getValue();
            switch (k.getKey()) {
            case "name":
                item.setName(val.getAsString());
                responses.addAll(responseSimpleSuccess("/resourcelinks/ " + id + "/name", item.getName()));
                break;
            case "type":
                item.setDescription(val.getAsString());
                responses.addAll(// w  w w .j  a va 2  s  .c  o m
                        responseSimpleSuccess("/resourcelinks/ " + id + "/description", item.getDescription()));
                break;
            case "classid":
                item.setClassid(val.getAsInt());
                responses
                        .addAll(responseSimpleSuccess("/resourcelinks/ " + id + "/classid", item.getClassid()));
                break;
            case "owner":
                item.setOwner(val.getAsString());
                responses.addAll(responseSimpleSuccess("/resourcelinks/ " + id + "/owner", item.getOwner()));
                break;
            case "recycle":
                item.setRecycle(val.getAsBoolean());
                responses
                        .addAll(responseSimpleSuccess("/resourcelinks/ " + id + "/recycle", item.getRecycle()));
                break;
            case "links":
                item.setLinks(Arrays.asList(JsonUtil.fromJson(val.getAsString(), String[].class)));
                responses.addAll(responseSimpleSuccess("/resourcelinks/ " + id + "/links", item.getLinks()));
                break;

            }
        });

        manager.onChange(id, item);
    }
    return responses;
}