Example usage for com.google.gson JsonObject get

List of usage examples for com.google.gson JsonObject get

Introduction

In this page you can find the example usage for com.google.gson JsonObject get.

Prototype

public JsonElement get(String memberName) 

Source Link

Document

Returns the member with the specified name.

Usage

From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java

License:Open Source License

private Map<Long, Domain> json2Domains(JsonObject json, Map<Long, FCAObject> objects,
        Map<Long, FCAAttribute> attributes, Map<Long, User> users, Map<Long, FCAItemMetadata> metadata) {

    Iterator<JsonElement> jsD = json.getAsJsonArray(SECTION_D).iterator();
    HashMap<Long, Domain> domains = new HashMap<Long, Domain>();
    while (jsD.hasNext()) {
        JsonObject d = jsD.next().getAsJsonObject();
        IncidenceMatrix mat = new IncidenceMatrix(d.get(NAME).getAsString(), d.get(DESCRIPTION).getAsString());
        Set<Entry<String, JsonElement>> mapping = d.get(MAPPING).getAsJsonObject().entrySet();
        JsonObject objMeta = d.get(SECTION_M).getAsJsonObject();
        for (Entry<String, JsonElement> map : mapping) {
            FCAObject object = objects.get(Long.parseLong(map.getKey()));
            FCAItemMetadata meta = metadata.get(objMeta.get(map.getKey()).getAsLong());
            System.out.println(object.getName());
            object.setLearningObjects(meta.getLearningObjects());
            object.setLearningObjectsByLearners(meta.getLearningObjectByLearner());
            String descr = object.getDescription();
            object.setDescription(meta.getDescription());
            mat.storeMetadata(object);//from  w ww .  j av  a  2s. co m
            object.setDescription(descr);
            Iterator<JsonElement> atts = map.getValue().getAsJsonArray().iterator();
            HashSet<FCAAttribute> attribs = new HashSet<FCAAttribute>();
            while (atts.hasNext()) {
                long id = atts.next().getAsLong();
                FCAAttribute attr = attributes.get(id);
                attribs.add(attr);
                meta = metadata.get(objMeta.get(Long.toString(id)).getAsLong());
                attr.setLearningObjects(meta.getLearningObjects());
                descr = attr.getDescription();
                attr.setDescription(meta.getDescription());
                mat.storeMetadata(attr);
                attr.setDescription(descr);
            }
            mat.add(object, attribs);
        }
        boolean global = (d.get(GLOBAL) == null) ? false : d.get(GLOBAL).getAsBoolean();
        Domain domain;
        // compat
        try {
            domain = new Domain(mat.getName(), mat.getDescription(), mat, users.get(d.get(OWNER).getAsLong()),
                    global);
        } catch (Exception e) {
            // new format
            Set<User> owners = new HashSet<User>();

            Iterator<JsonElement> uIds = d.getAsJsonArray(OWNER).iterator();
            while (uIds.hasNext())
                owners.add(users.get(uIds.next().getAsLong()));
            domain = new Domain(mat.getName(), mat.getDescription(), mat, owners, global);
        }

        if (d.get(APPROVED) == null) {
            domain.setApproved(true);
        } else {
            domain.setApproved(d.get(APPROVED).getAsBoolean());
        }
        domains.put(d.get(ID).getAsLong(), domain);
    }
    return domains;
}

From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java

License:Open Source License

private Set<Course> json2Courses(JsonObject json, Map<Long, Domain> domains, Map<Long, User> users) {
    Set<Course> courses = new HashSet<Course>();
    Iterator<JsonElement> jsC = json.getAsJsonArray(SECTION_C).iterator();
    while (jsC.hasNext()) {
        JsonObject c = jsC.next().getAsJsonObject();
        Iterator<JsonElement> domainIds = c.getAsJsonArray(SECTION_D).iterator();
        Set<Domain> ddomains = new HashSet<Domain>();
        while (domainIds.hasNext()) {
            Domain domain = domains.get(domainIds.next().getAsLong());
            ddomains.add(domain);/*from ww w  .  j  a  v  a  2 s .  co m*/
        }

        Iterator<JsonElement> userIds = c.getAsJsonArray(PARTICIPANTS).iterator();
        Set<User> uusers = new HashSet<User>();
        while (userIds.hasNext()) {
            User u = users.get(userIds.next().getAsLong());
            uusers.add(u);
        }
        Course course = new Course(c.get(NAME).getAsString(), c.get(DESCRIPTION).getAsString(),
                c.get(E_CID).getAsString());

        for (Domain d : ddomains) {
            course.addDomain(d);
        }
        for (User u : uusers)
            course.addParticipant(u);
        courses.add(course);
    }
    return courses;
}

From source file:at.yawk.buycraft.BuycraftApiImpl.java

License:Mozilla Public License

@Override
public InfoResponse info(String port, String onlineMode, String playerLimit, String version)
        throws IOException {
    Objects.requireNonNull(port);
    Objects.requireNonNull(onlineMode);
    Objects.requireNonNull(playerLimit);
    Objects.requireNonNull(version);
    JsonObject object = get("info",
            new URIBuilder().setParameter("serverPort", port).setParameter("onlineMode", onlineMode)
                    .setParameter("playersMax", playerLimit).setParameter("version", version));
    JsonObject payload = object.getAsJsonObject("payload");
    return new InfoResponse(payload.get("latestVersion").getAsDouble(),
            payload.get("latestDownload").getAsString(), payload.get("serverId").getAsInt(),
            payload.get("serverCurrency").getAsString(), payload.get("serverName").getAsString(),
            payload.get("serverStore").getAsString(), payload.get("updateUsernameInterval").getAsInt(),
            payload.get("onlineMode").getAsBoolean());
}

From source file:at.yawk.buycraft.BuycraftApiImpl.java

License:Mozilla Public License

@Override
public Set<Category> categories() throws IOException {
    JsonObject object = get("categories");
    Set<Category> categories = new HashSet<>();
    object.getAsJsonArray("payload").forEach(ele -> {
        JsonObject entry = ele.getAsJsonObject();
        Category category = new Category(entry.get("id").getAsInt(), entry.get("name").getAsString(),
                entry.get("shortDescription").getAsString(), entry.get("guiItemId").getAsInt(),
                entry.get("itemId").getAsString());
        categories.add(category);// w w w  .j a  v  a  2  s. com
    });
    return categories;
}

From source file:at.yawk.buycraft.BuycraftApiImpl.java

License:Mozilla Public License

@Override
public Set<Package> packages() throws IOException {
    JsonObject object = get("packages");
    Set<Package> packages = new HashSet<>();
    object.getAsJsonArray("payload").forEach(ele -> {
        JsonObject entry = ele.getAsJsonObject();
        Package pack = new Package(entry.get("id").getAsInt(), entry.get("order").getAsInt(),
                entry.get("name").getAsString(), entry.get("description").getAsString(),
                entry.get("shortDescription").getAsString(), entry.get("price").getAsString(),
                entry.get("category").getAsInt(), entry.get("guiItemId").getAsInt(),
                entry.get("itemId").getAsString());
        packages.add(pack);//from   ww w . j  a  va  2s .  c o m
    });
    return packages;
}

From source file:at.yawk.buycraft.BuycraftApiImpl.java

License:Mozilla Public License

@Override
public Set<Purchase> payments(String limit, Optional<String> user) throws IOException {
    URIBuilder builder = new URIBuilder();
    builder.setParameter("limit", limit);
    user.ifPresent(u -> builder.setParameter("ign", u));
    JsonObject object = get("payments", builder);
    Set<Purchase> purchases = new HashSet<>();
    object.getAsJsonArray("payload").forEach(ele -> {
        JsonObject entry = ele.getAsJsonObject();
        List<Integer> packages = new ArrayList<>();
        entry.getAsJsonArray("packages").forEach(pack -> packages.add(pack.getAsInt()));
        Purchase purchase = new Purchase(entry.get("time").getAsLong(), entry.get("humanTime").getAsString(),
                entry.get("ign").getAsString(), entry.get("uuid").getAsString(),
                entry.get("price").getAsString(), entry.get("currency").getAsString(),
                Collections.unmodifiableCollection(packages));
        purchases.add(purchase);//from www  .j a  v  a2 s . com
    });
    return purchases;
}

From source file:at.yawk.buycraft.BuycraftApiImpl.java

License:Mozilla Public License

@Override
public PendingResponse pending() throws IOException {
    JsonObject object = get("pendingUsers");
    List<String> pending = new ArrayList<>();
    JsonObject payload = object.getAsJsonObject("payload");
    payload.getAsJsonArray("pendingPlayers").forEach(ele -> pending.add(ele.getAsString()));
    return new PendingResponse(Collections.unmodifiableCollection(pending),
            payload.get("offlineCommands").getAsBoolean());
}

From source file:at.yawk.buycraft.BuycraftApiImpl.java

License:Mozilla Public License

@Override
public List<Command> commands(String players, String offlineCommands, String offlineCommandLimit)
        throws IOException {
    JsonObject object = get("commands",
            new URIBuilder().setParameter("do", "lookup").setParameter("users", players)
                    .setParameter("offlineCommands", offlineCommands)
                    .setParameter("offlineCommandLimit", offlineCommandLimit));
    List<Command> commands = new ArrayList<>();
    object.getAsJsonObject("payload").getAsJsonArray("commands").forEach(ele -> {
        JsonObject entry = ele.getAsJsonObject();
        Command command = new Command(entry.get("id").getAsInt(), entry.get("ign").getAsString(),
                entry.get("uuid").getAsString(), entry.get("command").getAsString(),
                entry.get("requireOnline").getAsBoolean(), entry.get("requireInventorySlot").getAsInt(),
                entry.get("delay").getAsInt());
        commands.add(command);/*from   w  w  w .  ja va 2s  .  c  om*/
    });
    return commands;
}

From source file:at.yawk.buycraft.web.HttpWebApi.java

License:Mozilla Public License

@Override
public void registerCoupon(Coupon coupon) throws IOException, CouponException {
    List<NameValuePair> reqPairs = new ArrayList<>();

    String csrf = loadCouponCsrf();
    reqPairs.add(new BasicNameValuePair("csrf", csrf));

    if (coupon.getBasketTypes().contains(BasketType.SINGLE)) {
        if (coupon.getBasketTypes().contains(BasketType.SUBSCRIPTION)) {
            reqPairs.add(new BasicNameValuePair("basket_type", "both"));
        } else {/*from  w  w  w. j av a2  s  . com*/
            reqPairs.add(new BasicNameValuePair("basket_type", "subscription"));
        }
    } else {
        // without basket type would be invalid, should be checked in CouponBuilder
        reqPairs.add(new BasicNameValuePair("basket_type", "single"));
    }
    reqPairs.add(new BasicNameValuePair("code", coupon.getCode()));
    switch (coupon.getDiscountType()) {
    case AMOUNT:
        reqPairs.add(new BasicNameValuePair("discount_type", "value"));
        reqPairs.add(
                new BasicNameValuePair("discount_amount", String.format("%.2f", coupon.getDiscountAmount())));
        reqPairs.add(new BasicNameValuePair("discount_percentage", "0"));
        break;
    case PERCENTAGE:
        reqPairs.add(new BasicNameValuePair("discount_type", "percentage"));
        reqPairs.add(new BasicNameValuePair("discount_amount", "0.00"));
        reqPairs.add(new BasicNameValuePair("discount_percentage",
                Double.toString(coupon.getDiscountFraction() * 100)));
        break;
    }

    switch (coupon.getExpiryType()) {
    case NEVER:
        reqPairs.add(new BasicNameValuePair("expire_type", "never"));
        reqPairs.add(new BasicNameValuePair("expire_limit", "0"));
        reqPairs.add(new BasicNameValuePair("expire_timestamp", LocalDate.now().format(DATE_FORMATTER)));
        break;
    case DATE:
        reqPairs.add(new BasicNameValuePair("expire_type", "timestamp"));
        reqPairs.add(new BasicNameValuePair("expire_timestamp", coupon.getExpiryDate().format(DATE_FORMATTER)));
        reqPairs.add(new BasicNameValuePair("expire_limit", "0"));
        break;
    case REDEEM_COUNT:
        reqPairs.add(new BasicNameValuePair("expire_type", "limit"));
        reqPairs.add(new BasicNameValuePair("expire_limit", Integer.toString(coupon.getExpiryRedeemCount())));
        reqPairs.add(new BasicNameValuePair("expire_timestamp", LocalDate.now().format(DATE_FORMATTER)));
        break;
    }

    reqPairs.add(new BasicNameValuePair("minimum", String.format("%.2f", coupon.getMinimumBasketValue())));
    reqPairs.add(new BasicNameValuePair("start_time", coupon.getStartDate().format(DATE_FORMATTER)));
    reqPairs.add(new BasicNameValuePair("user_limit", Integer.toString(coupon.getMaximumUserUses())));

    switch (coupon.getEffectType()) {
    case CART:
        reqPairs.add(new BasicNameValuePair("type", "cart"));
        break;
    case CATEGORIES:
        for (int category : coupon.getCategoryIds()) {
            reqPairs.add(new BasicNameValuePair("categories[]", Integer.toString(category)));
        }
        reqPairs.add(new BasicNameValuePair("type", "category"));
        break;
    case PACKAGES:
        for (int packageId : coupon.getPackages()) {
            reqPairs.add(new BasicNameValuePair("packages[]", Integer.toString(packageId)));
        }
        reqPairs.add(new BasicNameValuePair("type", "package"));
        break;
    }

    HttpPost request = new HttpPost("https://server.buycraft.net/coupons/add");
    request.addHeader("X-Requested-With", "XMLHttpRequest");
    request.setEntity(new UrlEncodedFormEntity(reqPairs));
    HttpResponse response = client.execute(request);

    HttpEntity entity = response.getEntity();
    Header encoding = entity.getContentEncoding();
    JsonObject json = new JsonParser()
            .parse(new InputStreamReader(entity.getContent(), encoding == null ? "UTF-8" : encoding.getValue()))
            .getAsJsonObject();

    if (json.get("type").getAsString().equals("error")) {
        throw new CouponException(json.get("message").toString());
    }
}

From source file:at.yawk.votifier.VotifierKeyPair.java

License:Mozilla Public License

public static VotifierKeyPair read(JsonObject source) throws Exception {
    byte[] publicKeyEnc = Base64.getDecoder().decode(source.get("public_key").getAsString());
    byte[] privateKeyEnc = Base64.getDecoder().decode(source.get("private_key").getAsString());

    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyEnc));
    PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyEnc));

    return new VotifierKeyPair(new KeyPair(publicKey, privateKey));
}