• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "util/json/json_value.h"
6 
7 namespace openscreen {
8 
MaybeGetInt(const Json::Value & message,const char * first,const char * last)9 absl::optional<int> MaybeGetInt(const Json::Value& message,
10                                 const char* first,
11                                 const char* last) {
12   const Json::Value* value = message.find(first, last);
13   absl::optional<int> result;
14   if (value && value->isInt()) {
15     result = value->asInt();
16   }
17   return result;
18 }
19 
MaybeGetString(const Json::Value & message)20 absl::optional<absl::string_view> MaybeGetString(const Json::Value& message) {
21   if (message.isString()) {
22     const char* begin = nullptr;
23     const char* end = nullptr;
24     message.getString(&begin, &end);
25     if (begin && end >= begin) {
26       return absl::string_view(begin, end - begin);
27     }
28   }
29   return absl::nullopt;
30 }
31 
MaybeGetString(const Json::Value & message,const char * first,const char * last)32 absl::optional<absl::string_view> MaybeGetString(const Json::Value& message,
33                                                  const char* first,
34                                                  const char* last) {
35   const Json::Value* value = message.find(first, last);
36   absl::optional<absl::string_view> result;
37   if (value && value->isString()) {
38     return MaybeGetString(*value);
39   }
40   return result;
41 }
42 
43 }  // namespace openscreen
44