GIDJSONSerializerImpl.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright 2025 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "GoogleSignIn/Sources/GIDJSONSerializer/Implementation/GIDJSONSerializerImpl.h"
  17. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
  18. NSString * const kGIDJSONSerializationErrorDescription =
  19. @"The provided object could not be serialized to a JSON string.";
  20. @implementation GIDJSONSerializerImpl
  21. - (nullable NSString *)stringWithJSONObject:(NSDictionary<NSString *, id> *)jsonObject
  22. error:(NSError *_Nullable *_Nullable)error {
  23. NSError *serializationError;
  24. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject
  25. options:0
  26. error:&serializationError];
  27. if (!jsonData) {
  28. if (error) {
  29. *error = [NSError errorWithDomain:kGIDSignInErrorDomain
  30. code:kGIDSignInErrorCodeJSONSerializationFailure
  31. userInfo:@{
  32. NSLocalizedDescriptionKey:kGIDJSONSerializationErrorDescription,
  33. NSUnderlyingErrorKey:serializationError
  34. }];
  35. }
  36. return nil;
  37. }
  38. return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  39. }
  40. @end