Update to serde_json 1.0 (#6)

* Replace find() with get()

* Update for renamed as_string->as_str and as_boolean->as_bool

https://github.com/serde-rs/json/releases/tag/v0.8.0
Value::as_string() has been renamed to as_str() and Value::as_boolean() has been renamed to as_bool() to improve consistency
https://github.com/serde-rs/json/issues/126

* No serde_json::Value::I64/U64/F64 anymore, only Number

* Update from lookup() to pointer(), using JSON pointer syntax

https://github.com/iceiix/steven/pull/6#issuecomment-432472123

* Remove unused and removed ObjectBuilder import

* Use into_iter().collect() to convert BTreeMap to serde_json::Map

* Change parse_rules to accept serde_json::Map instead of BTreeMap

* Remove unused serde_json macro_use

* Update Cargo.lock
This commit is contained in:
iceiix 2018-10-23 18:47:21 -07:00 committed by GitHub
parent ca79b0c735
commit b2b1ec00ed
2 changed files with 35 additions and 37 deletions

View File

@ -906,29 +906,29 @@ impl Conn {
let invalid_status = || Error::Err("Invalid status".to_owned());
let version = try!(val.find("version").ok_or(invalid_status()));
let players = try!(val.find("players").ok_or(invalid_status()));
let version = try!(val.get("version").ok_or(invalid_status()));
let players = try!(val.get("players").ok_or(invalid_status()));
Ok((Status {
version: StatusVersion {
name: try!(version.find("name").and_then(Value::as_string).ok_or(invalid_status()))
name: try!(version.get("name").and_then(Value::as_str).ok_or(invalid_status()))
.to_owned(),
protocol: try!(version.find("protocol")
protocol: try!(version.get("protocol")
.and_then(Value::as_i64)
.ok_or(invalid_status())) as i32,
},
players: StatusPlayers {
max: try!(players.find("max")
max: try!(players.get("max")
.and_then(Value::as_i64)
.ok_or(invalid_status())) as i32,
online: try!(players.find("online")
online: try!(players.get("online")
.and_then(Value::as_i64)
.ok_or(invalid_status())) as i32,
sample: Vec::new(), /* TODO */
},
description: format::Component::from_value(try!(val.find("description")
description: format::Component::from_value(try!(val.get("description")
.ok_or(invalid_status()))),
favicon: val.find("favicon").and_then(Value::as_string).map(|v| v.to_owned()),
favicon: val.get("favicon").and_then(Value::as_str).map(|v| v.to_owned()),
},
ping))
}

View File

@ -14,7 +14,6 @@
use sha1::{self, Digest};
use serde_json;
use serde_json::builder::ObjectBuilder;
use hyper;
#[derive(Clone, Debug)]
@ -31,15 +30,14 @@ const VALIDATE_URL: &'static str = "https://authserver.mojang.com/validate";
impl Profile {
pub fn login(username: &str, password: &str, token: &str) -> Result<Profile, super::Error> {
let req_msg = ObjectBuilder::new()
.insert("username", username)
.insert("password", password)
.insert("clientToken", token)
.insert_object("agent", |b| b
.insert("name", "Minecraft")
.insert("version", 1)
)
.unwrap();
let req_msg = json!({
"username": username,
"password": password,
"clientToken": token,
"agent": {
"name": "Minecraft",
"version": 1
}});
let req = try!(serde_json::to_string(&req_msg));
let client = hyper::Client::new();
@ -49,25 +47,25 @@ impl Profile {
.send());
let ret: serde_json::Value = try!(serde_json::from_reader(res));
if let Some(error) = ret.find("error").and_then(|v| v.as_string()) {
if let Some(error) = ret.get("error").and_then(|v| v.as_str()) {
return Err(super::Error::Err(format!(
"{}: {}",
error,
ret.find("errorMessage").and_then(|v| v.as_string()).unwrap())
ret.get("errorMessage").and_then(|v| v.as_str()).unwrap())
));
}
Ok(Profile {
username: ret.lookup("selectedProfile.name").and_then(|v| v.as_string()).unwrap().to_owned(),
id: ret.lookup("selectedProfile.id").and_then(|v| v.as_string()).unwrap().to_owned(),
access_token: ret.find("accessToken").and_then(|v| v.as_string()).unwrap().to_owned(),
username: ret.pointer("/selectedProfile/name").and_then(|v| v.as_str()).unwrap().to_owned(),
id: ret.pointer("/selectedProfile/id").and_then(|v| v.as_str()).unwrap().to_owned(),
access_token: ret.get("accessToken").and_then(|v| v.as_str()).unwrap().to_owned(),
})
}
pub fn refresh(self, token: &str) -> Result<Profile, super::Error> {
let req_msg = ObjectBuilder::new()
.insert("accessToken", self.access_token.clone())
.insert("clientToken", token)
.unwrap();
let req_msg = json!({
"accessToken": self.access_token.clone(),
"clientToken": token
});
let req = try!(serde_json::to_string(&req_msg));
let client = hyper::Client::new();
@ -84,17 +82,17 @@ impl Profile {
.send());
let ret: serde_json::Value = try!(serde_json::from_reader(res));
if let Some(error) = ret.find("error").and_then(|v| v.as_string()) {
if let Some(error) = ret.get("error").and_then(|v| v.as_str()) {
return Err(super::Error::Err(format!(
"{}: {}",
error,
ret.find("errorMessage").and_then(|v| v.as_string()).unwrap())
ret.get("errorMessage").and_then(|v| v.as_str()).unwrap())
));
}
return Ok(Profile {
username: ret.lookup("selectedProfile.name").and_then(|v| v.as_string()).unwrap().to_owned(),
id: ret.lookup("selectedProfile.id").and_then(|v| v.as_string()).unwrap().to_owned(),
access_token: ret.find("accessToken").and_then(|v| v.as_string()).unwrap().to_owned(),
username: ret.pointer("/selectedProfile/name").and_then(|v| v.as_str()).unwrap().to_owned(),
id: ret.pointer("/selectedProfile/id").and_then(|v| v.as_str()).unwrap().to_owned(),
access_token: ret.get("accessToken").and_then(|v| v.as_str()).unwrap().to_owned(),
});
}
Ok(self)
@ -121,11 +119,11 @@ impl Profile {
hash_val.to_owned()
};
let join_msg = ObjectBuilder::new()
.insert("accessToken", &self.access_token)
.insert("selectedProfile", &self.id)
.insert("serverId", hash_str)
.unwrap();
let join_msg = json!({
"accessToken": &self.access_token,
"selectedProfile": &self.id,
"serverId": hash_str
});
let join = serde_json::to_string(&join_msg).unwrap();
let client = hyper::Client::new();