दुर्भाग्यवश मुझे एक्सएमएल में ऐसा करने के लिए वास्तव में कोई अच्छी लाइब्रेरी नहीं पता (हालांकि Anti-Xml एक नज़र डालने लायक है)। लेकिन वहाँ Json के लिए इस तरह के पुस्तकालय हैं और यह, PlayJson खेलने json के साथ आप मूल रूप से सब कुछ आप रूबी के JsBuilder साथ कर सकते हैं क्या कर सकते हैं कुछ प्रतिमान अंतर को छोड़कर:
यहाँ उदाहरण कोड है:
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
case class Attachment(fileName: String, url: String)
case class Author(name: String, email: String, url: String)
case class Comment(content: String, created_at: DateTime)
case class Post(author: Author, content: String, attachments: List[Attachment], comments: List[Comment], created_at: DateTime, updated_at: DateTime)
object Main {
import play.api.libs.json._
import play.api.libs.functional.syntax._
val isAdmin = true
def calculateVisits(post: Post) = 35
implicit val jodaTimeWrites: Writes[DateTime] = new Writes[DateTime] {
def writes(c: DateTime): JsValue = {
Json.toJson(c.toString(DateTimeFormat.fullDateTime()))
}
}
implicit val attachmentFormat = Json.format[Attachment]
implicit val authorWrites: Writes[Author] = (
(__ \ "name").write[String] and
(__ \ "email").write[String] and
(__ \ "url").write[String]) { unlift(Author.unapply) }
implicit val commentWrites: Writes[Comment] = (
(__ \ "content").write[String] and
(__ \ "created_at").write[DateTime]) { unlift(Comment.unapply) }
implicit val postWrites: Writes[Post] = (
(__ \ "content").write[String] and
(__ \ "created_at").write[DateTime] and
(__ \ "updated_at").write[DateTime] and
(__ \ "author").write[Author] and
(__ \ "visitors").write[Option[Int]] and
(__ \ "comments").write[List[Comment]] and
(__ \ "attachments").write[List[Attachment]]) { post: Post =>
(
post.content,
post.created_at,
post.updated_at,
post.author,
if (isAdmin) Some(calculateVisits(post)) else None,
post.comments,
post.attachments)
}
def main(args: Array[String]): Unit = {
val post = Post(
Author("David H.", "[email protected]", "http://example.com/users/1-david.json"),
"<p>This is <i>serious</i> monkey business</p>",
List(
Attachment("forecast.xls", "http://example.com/downloads/forecast.xls"),
Attachment("presentation.pdf", "http://example.com/downloads/presentation.pdf")),
List(
Comment("Hello everyone!", new DateTime()),
Comment("To you my good sir!", new DateTime())),
new DateTime(),
new DateTime())
Console println (Json prettyPrint (Json toJson post))
}
}
सूचना attachmentFormat
- यह स्केला मैक्रो द्वारा उत्पन्न होता है मामले वर्ग परिभाषा के साथ मिलान किया जाना है। मेरी परियोजना में मैंने कभी भी एक मैनुअल प्रारूप ओवरराइड संकलक नहीं लिखा है मेरे लिए सभी प्रारूप उत्पन्न करता है! लेकिन अगर मुझे जरूरत है तो मैं कर सकता हूं। अच्छा उदाहरण jodaTimeWrites
है - डिफ़ॉल्ट जोडाटाइमफॉर्मैट लंबे मूल्य उत्पन्न करेगा जो मशीन प्रसंस्करण के लिए अधिक उपयुक्त है, लेकिन मैंने इसे रूबी के नमूने से मेल खाने के लिए अपने स्वयं के निहित प्रारूप के साथ ओवरराइड कर दिया है।
कोड ऊपर उत्पादन निम्नलिखित का उत्पादन: गहरे लाल रंग का कोड की
अब 21 के बजाय {
"content" : "<p>This is <i>serious</i> monkey business</p>",
"created_at" : "Friday, July 5, 2013 4:19:42 PM +03:00",
"updated_at" : "Friday, July 5, 2013 4:19:42 PM +03:00",
"author" : {
"name" : "David H.",
"email" : "[email protected]",
"url" : "http://example.com/users/1-david.json"
},
"visitors" : 35,
"comments" : [ {
"content" : "Hello everyone!",
"created_at" : "Friday, July 5, 2013 4:19:42 PM +03:00"
}, {
"content" : "To you my good sir!",
"created_at" : "Friday, July 5, 2013 4:19:42 PM +03:00"
} ],
"attachments" : [ {
"fileName" : "forecast.xls",
"url" : "http://example.com/downloads/forecast.xls"
}, {
"fileName" : "presentation.pdf",
"url" : "http://example.com/downloads/presentation.pdf"
} ]
}
लाइनों मैं किसी भी तरह अधिक जटिल मैपिंग स्केला के 33 लाइनों मिल गया (जैसा भी मामला वर्गों के बिना)। और क्यों टाइप करें? क्योंकि अब मैं निश्चित रूप से मर चुका हूं कि जब मैं Attachment
के बजाय Comment
पास करता हूं तो मुझे कंपाइलर त्रुटि मिल जाएगी, या जब मेरे सहयोगी गलती से बदल जाएंगे joda.time.DateFormat java.util.DateFormat को कुछ गड़बड़ के बजाय त्रुटि प्राप्त होगी ।