当前位置: 首页>>代码示例>>Scala>>正文


Scala JsonObject类代码示例

本文整理汇总了Scala中com.google.gson.JsonObject的典型用法代码示例。如果您正苦于以下问题:Scala JsonObject类的具体用法?Scala JsonObject怎么用?Scala JsonObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了JsonObject类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: Message

//设置package包名称以及导入依赖的类
package org.eck.entities

import java.security.MessageDigest

import com.google.appengine.api.datastore.{Key, Entity, KeyFactory, DatastoreServiceFactory}
import com.google.gson.JsonObject

class Message(var title: String, var content: String) {

  def save: String = {
    DatastoreServiceFactory.getDatastoreService.put(toEntity)
    id
  }

  def toEntity: Entity = {
    val entity = new Entity(Message.key(id))
    entity.setProperty("title", title)
    entity.setProperty("content", content)
    entity
  }

  def toJson: JsonObject = {
    val json = new JsonObject
    json.addProperty("title", title)
    json.addProperty("content", content)
    json
  }

  def id : String = new String(MessageDigest.getInstance("MD5").digest((title + content).getBytes))
}

object Message {

  def key(id: String): Key = KeyFactory.createKey("MESSAGES", id)

  def fromEntity(entity: Entity) : Message = {
    val title = entity.getProperty("title").asInstanceOf[String]
    val content = entity.getProperty("content").asInstanceOf[String]
    new Message(title, content)
  }

  def fromJson(json: JsonObject): Message = {
    val title = json.get("title").getAsString
    val content = json.get("content").getAsString
    new Message(title, content)
  }

  def findById(id: String): Message = Message.fromEntity(DatastoreServiceFactory.getDatastoreService.get(Message.key(id)))
} 
开发者ID:erickzanardo,项目名称:spammer,代码行数:50,代码来源:Message.scala

示例2: wrapperObject

//设置package包名称以及导入依赖的类
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser

case class wrapperObject(val json_string: Array[MyJsonObject])
case class MyJsonObject(val id:Int ,val price:Int)

object JsonParsing {
  
    def maxVal():Integer = {
      
        val json_string = scala.io.Source.fromFile("jsonData1.json").getLines.mkString
        val jsonStringAsObject= new JsonParser().parse(json_string).getAsJsonObject
        val myObj:wrapperObject = gson.fromJson(jsonStringAsObject, classOf[wrapperObject])
        var maxPrice:Int = 0
        for(i <- myObj.json_string if i.price > maxPrice) 
        {
            maxPrice=  i.price
        }
        return maxPrice
    }
  
    def time[R](block: => R): R = {  
        val t0 = System.nanoTime()
        val result = block    // call-by-name
        val t1 = System.nanoTime()
        println("Elapsed time: " + (t1 - t0) + "ns")
        result
    }

    val gson = new Gson()
    
    def main(args: Array[String]): Unit = {
       
      time{println(maxVal())}
      
      val runtime = Runtime.getRuntime
      println("Used Memory:  " + (runtime.totalMemory - runtime.freeMemory))    
    }
} 
开发者ID:vinzee,项目名称:language_comparison,代码行数:41,代码来源:JsonParsing.scala

示例3: MCWhitelistHelper

//设置package包名称以及导入依赖的类
package com.amadornes.modcast.bot.helpers

import java.net.URLEncoder

import com.amadornes.modcast.bot.Actors
import com.amadornes.modcast.bot.database.{DB, UserMCAccount}
import com.amadornes.modcast.bot.servers.MCWhitelistServer
import com.google.gson.{Gson, JsonObject}
import sx.blah.discord.handle.obj.IUser

import scalaj.http.Http


object MCWhitelistHelper {
	def associateMCAccountWithUser(user: IUser, account: String): Unit = {
		val id = getMCAccountUUID(account)
		if (DB.query[UserMCAccount].whereEqual("user", user.getID).exists())
			deassociateMCAccountWithUser(user)
		Actors.servers.MCWhitelistServer ! MCWhitelistServer.WhitelistUser(id)
		DB.save(UserMCAccount(user.getID, id))
	}
	
	def deassociateMCAccountWithUser(user: IUser): Unit = {
		val account = DB.query[UserMCAccount].whereEqual("user", user.getID).fetchOne()
		if (account.isDefined) {
			Actors.servers.MCWhitelistServer ! MCWhitelistServer.UnWhitelistUser(account.get.account)
			DB.delete(account.get)
		}
	}
	
	def getMCAccountUUID(name: String): String = {
		val http = Http("http://mcapi.ca/profile/" + URLEncoder.encode(name)).asString
		if (http.code != 200)
			throw new IllegalArgumentException()
		new Gson().fromJson(http.body, classOf[JsonObject]).get("uuid_formatted").getAsString
	}
} 
开发者ID:Modcast,项目名称:ModcastBot,代码行数:38,代码来源:MCWhitelistHelper.scala

示例4: Settings

//设置package包名称以及导入依赖的类
package org.eck.entities

import com.google.appengine.api.datastore._
import com.google.gson.JsonObject

class Settings {

}

object Settings {
  val entityName = "SETTINGS"

  def asJson: JsonObject = {
    val ds = DatastoreServiceFactory.getDatastoreService
    val iterator = ds.prepare(new Query(entityName)).asIterator()

    val json = new JsonObject

    while (iterator.hasNext) {
      val entity = iterator.next
      val key = entity.getKey.getName
      val value = entity.getProperty("value").asInstanceOf[String]
      json.addProperty(key, value)
    }

    json
  }

  def fromJson(json: JsonObject) = {
    val ds = DatastoreServiceFactory.getDatastoreService

    val entrySet = json.entrySet()
    val iterator = entrySet.iterator()

    while(iterator.hasNext) {
      val entry = iterator.next
      val entity = new Entity(KeyFactory.createKey(entityName, entry.getKey))
      entity.setProperty("value", entry.getValue.getAsString)
      ds.put(entity)
    }
  }

  def set(key: String, value: String) = {
    val ds = DatastoreServiceFactory.getDatastoreService
    val entity = new Entity(KeyFactory.createKey(entityName, key))
    entity.setProperty("value", value)
    ds.put(entity)
  }

  def get(key: String): Option[String] = {
    val ds = DatastoreServiceFactory.getDatastoreService
    try {
      val entity = ds.get(KeyFactory.createKey(entityName, key))
      return Option(entity.getProperty("value").asInstanceOf[String])
    } catch {
      case e: EntityNotFoundException => return None
    }
  }
} 
开发者ID:erickzanardo,项目名称:spammer,代码行数:60,代码来源:Settings.scala

示例5: UserMessage

//设置package包名称以及导入依赖的类
package org.eck.entities

import com.google.gson.{JsonPrimitive, JsonNull, JsonObject}
import org.eck.utils.DateUtils

class UserMessage(val notification: Notification, val message: Message) {

  def toJson: JsonObject = {
    val json = new JsonObject
    json.addProperty("id", notification.id.get)
    json.addProperty("title", message.title)
    json.addProperty("content", message.content)
    json.addProperty("created_at", DateUtils.format(notification.createdAt))

    val readDateJson = if(notification.readDate.isDefined) {
      new JsonPrimitive(DateUtils.format(notification.readDate.get))
    } else {
      JsonNull.INSTANCE
    }

    json.add("read_date", readDateJson)

    json
  }
} 
开发者ID:erickzanardo,项目名称:spammer,代码行数:26,代码来源:UserMessage.scala

示例6: HttpUtil

//设置package包名称以及导入依赖的类
package org.eck.utils

import javax.servlet.http.{HttpServletResponse, HttpServletRequest}

import com.google.gson.{JsonElement, JsonParser, JsonObject}

object HttpUtil {
  def readBody(request: HttpServletRequest): String = {
    val reader = request.getReader
    val sb = new StringBuilder
    var line = ""
    while({line = reader.readLine();  line != null}) sb.append(line)
    sb.toString
  }

  def readBodyAsJson(request: HttpServletRequest): JsonObject = {
    val parser = new JsonParser
    parser.parse(readBody(request)).getAsJsonObject
  }

  def writeJson(response: HttpServletResponse, json: JsonElement) = {
    response.addHeader("Content-Type", "application/json")
    response.getWriter.print(json.toString)
  }
} 
开发者ID:erickzanardo,项目名称:spammer,代码行数:26,代码来源:HttpUtil.scala

示例7: SettingsTest

//设置package包名称以及导入依赖的类
package org.eck.entities

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig
import com.google.appengine.tools.development.testing.LocalServiceTestHelper
import com.google.gson.JsonObject
import org.junit.{Assert, Test, After, Before}

class SettingsTest {
  val helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig())

  @Before def setUp(): Unit = helper.setUp
  @After def tearDown() = helper.tearDown()

  @Test
  def testSetAndGetSetting = {
    Settings.set("bla", "ble")
    Assert.assertTrue(Settings.get("bla").isDefined)
    Assert.assertEquals("ble", Settings.get("bla").get)
  }

  @Test
  def testFromJson = {
    val json = new JsonObject
    json.addProperty("bla", "ble")
    json.addProperty("ble", "bla")

    Settings.fromJson(json)

    Assert.assertTrue(Settings.get("bla").isDefined)
    Assert.assertEquals("ble", Settings.get("bla").get)

    Assert.assertTrue(Settings.get("ble").isDefined)
    Assert.assertEquals("bla", Settings.get("ble").get)
  }

  @Test
  def asJson = {
    Settings.set("bla", "ble")
    Settings.set("ble", "bla")

    val json = Settings.asJson

    Assert.assertEquals("ble", json.get("bla").getAsString)
    Assert.assertEquals("bla", json.get("ble").getAsString)
  }
} 
开发者ID:erickzanardo,项目名称:spammer,代码行数:47,代码来源:SettingsTest.scala


注:本文中的com.google.gson.JsonObject类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。