Common Scala pattern matching

Source: Internet
Author: User

The following are some of the minor issues that I encountered at work today:

1. because the business needs to call the PHP interface, the returned body needs to be processed logically. However, the parameter received by a field is Io. serializable type. The field type is not the expected string type. At that time, it was a little awesome because it was a pattern match using Scala's match case. I didn't think much about it. Thanks to my colleague, I remember that string actually implements the serializable interface, even though I/O is returned. serializable type, but can also be received using string;

2. call the PHP interface and accept the response body using jsonobject. However, some fields are null. The instinct is to convert gson to an object and use Scala's option to receive fields that may be null, however, when the program runs, an error with no ARGs will be reported;

Solution: Use Case class to receive responses from the PHP interface. You do not need to add option to a field that may be null. Instead, add isjsonnull to the field in jsonobject, when the following build thrift returns the body, these fields are defined as the build of the Option type;

In fact, it is okay to use case class to determine and obtain fields. We will conduct an experiment tomorrow;

Val uesrname: Option [String] = response. get ("data "). getasjsonobject. get ("username "). isjsonnull match {case true => none case false => some (response. get ("data "). getasjsonobject. get ("username "). getasstring )}

 

Common Scala pattern matching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.