FLEXArgumentInputJSONObjectView.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // FLEXArgumentInputJSONObjectView.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/15/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXArgumentInputJSONObjectView.h"
  9. #import "FLEXRuntimeUtility.h"
  10. @implementation FLEXArgumentInputJSONObjectView
  11. - (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding
  12. {
  13. self = [super initWithArgumentTypeEncoding:typeEncoding];
  14. if (self) {
  15. // Start with the numbers and punctuation keyboard since quotes, curly braces, or
  16. // square brackets are likely to be the first characters type for the JSON.
  17. self.inputTextView.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
  18. }
  19. return self;
  20. }
  21. - (void)setInputOutput:(id)inputOutput
  22. {
  23. self.inputTextView.text = [FLEXRuntimeUtility editableJSONStringForObject:inputOutput];
  24. }
  25. - (id)inputOutput
  26. {
  27. return [FLEXRuntimeUtility objectValueFromEditableJSONString:self.inputTextView.text];
  28. }
  29. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value
  30. {
  31. // Must be object type.
  32. BOOL supported = type[0] == '@';
  33. if (supported) {
  34. if (value) {
  35. // If there's a current value, it must be serializable to JSON
  36. supported = [FLEXRuntimeUtility editableJSONStringForObject:value] != nil;
  37. } else {
  38. // Otherwise, see if we have more type information than just 'id'.
  39. // If we do, make sure the encoding is something serializable to JSON.
  40. // Properties and ivars keep more detailed type encoding information than method arguments.
  41. if (strcmp(type, @encode(id)) != 0) {
  42. BOOL isJSONSerializableType = NO;
  43. // Note: we can't use @encode(NSString) here because that drops the string information and just goes to @encode(id).
  44. isJSONSerializableType = isJSONSerializableType || strcmp(type, "@\"NSString\"") == 0;
  45. isJSONSerializableType = isJSONSerializableType || strcmp(type, "@\"NSNumber\"") == 0;
  46. isJSONSerializableType = isJSONSerializableType || strcmp(type, "@\"NSArray\"") == 0;
  47. isJSONSerializableType = isJSONSerializableType || strcmp(type, "@\"NSDictionary\"") == 0;
  48. supported = isJSONSerializableType;
  49. }
  50. }
  51. }
  52. return supported;
  53. }
  54. @end